Skip to content

Commit 92733d6

Browse files
committedApr 5, 2019
BIG UPDATE: New models functions for animations!
Multiple functions added and some reviewed to adapt to the new multi-mesh, multi-material and animated models.
1 parent 38a13b7 commit 92733d6

File tree

8 files changed

+543
-932
lines changed

8 files changed

+543
-932
lines changed
 

‎examples/others/iqm_loader/models_iqm_animation.c ‎examples/models/models_animation.c

+27-24
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
/*******************************************************************************************
22
*
3-
* raylib [models] example - Load IQM 3d model with animations and play them
3+
* raylib [models] example - Load 3d model with animations and play them
44
*
5-
* This example has been created using raylib 2.0 (www.raylib.com)
5+
* This example has been created using raylib 2.5 (www.raylib.com)
66
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
77
*
8-
* Copyright (c) 2018 @culacant and @raysan5
8+
* Copyright (c) 2019 Ramon Santamaria (@raysan5) and @culacant
99
*
1010
********************************************************************************************/
1111

1212
#include "raylib.h"
1313

14-
#define RIQM_IMPLEMENTATION
15-
#include "riqm.h"
16-
1714
int main()
1815
{
1916
// Initialization
2017
//--------------------------------------------------------------------------------------
2118
int screenWidth = 800;
2219
int screenHeight = 450;
2320

24-
InitWindow(screenWidth, screenHeight, "raylib [models] example - iqm animation");
21+
InitWindow(screenWidth, screenHeight, "raylib [models] example - model animation");
2522

2623
// Define the camera to look into our 3d world
2724
Camera camera = { 0 };
@@ -31,26 +28,25 @@ int main()
3128
camera.fovy = 45.0f; // Camera field-of-view Y
3229
camera.type = CAMERA_PERSPECTIVE; // Camera mode type
3330

34-
// Load the animated model mesh and basic data
35-
AnimatedModel model = LoadAnimatedModel("resources/guy.iqm");
3631

37-
// Load model texture and set material
38-
// NOTE: There is only 1 mesh and 1 material (both at index 0), thats what the 2 0's are
39-
model = AnimatedModelAddTexture(model, "resources/guytex.png"); // REPLACE!
40-
model = SetMeshMaterial(model, 0, 0); // REPLACE!
32+
Model model = LoadModel("resources/guy/guy.iqm"); // Load the animated model mesh and basic data
33+
Texture2D texture = LoadTexture("resources/guy/guytex.png"); // Load model texture and set material
34+
SetMaterialTexture(&model.materials[0], MAP_DIFFUSE, texture); // Set model material map texture
35+
36+
Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
4137

4238
// Load animation data
43-
Animation anim = LoadAnimationFromIQM("resources/guyanim.iqm");
44-
39+
int animsCount = 0;
40+
ModelAnimation *anims = LoadModelAnimations("resources/guy/guyanim.iqm", &animsCount);
4541
int animFrameCounter = 0;
4642

47-
SetCameraMode(camera, CAMERA_FREE); // Set free camera mode
43+
SetCameraMode(camera, CAMERA_FREE); // Set free camera mode
4844

49-
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
45+
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
5046
//--------------------------------------------------------------------------------------
5147

5248
// Main game loop
53-
while (!WindowShouldClose()) // Detect window close button or ESC key
49+
while (!WindowShouldClose()) // Detect window close button or ESC key
5450
{
5551
// Update
5652
//----------------------------------------------------------------------------------
@@ -60,7 +56,8 @@ int main()
6056
if (IsKeyDown(KEY_SPACE))
6157
{
6258
animFrameCounter++;
63-
AnimateModel(model, anim, animFrameCounter); // Animate the model with animation data and frame
59+
UpdateModelAnimation(model, anims[0], animFrameCounter);
60+
if (animFrameCounter >= anims[0].frameCount) animFrameCounter = 0;
6461
}
6562
//----------------------------------------------------------------------------------
6663

@@ -72,14 +69,18 @@ int main()
7269

7370
BeginMode3D(camera);
7471

75-
DrawAnimatedModel(model, Vector3Zero(), 1.0f, WHITE); // Draw animated model
72+
DrawModelEx(model, position, (Vector3){ 1.0f, 0.0f, 0.0f }, -90.0f, (Vector3){ 1.0f, 1.0f, 1.0f }, WHITE);
73+
74+
for (int i = 0; i < model.boneCount; i++)
75+
{
76+
DrawCube(anims[0].framePoses[animFrameCounter][i].translation, 0.2f, 0.2f, 0.2f, RED);
77+
}
7678

7779
DrawGrid(10, 1.0f); // Draw a grid
7880

7981
EndMode3D();
80-
81-
DrawText("PRESS SPACE to PLAY IQM MODEL ANIMATION", 10, 10, 20, MAROON);
8282

83+
DrawText("PRESS SPACE to PLAY MODEL ANIMATION", 10, 10, 20, MAROON);
8384
DrawText("(c) Guy IQM 3D model by @culacant", screenWidth - 200, screenHeight - 20, 10, GRAY);
8485

8586
EndDrawing();
@@ -88,8 +89,10 @@ int main()
8889

8990
// De-Initialization
9091
//--------------------------------------------------------------------------------------
91-
UnloadAnimation(anim); // Unload animation data
92-
UnloadAnimatedModel(model); // Unload animated model
92+
// Unload model animations data
93+
for (int i = 0; i < animsCount; i++) UnloadModelAnimation(anims[i]);
94+
95+
UnloadModel(model); // Unload model
9396

9497
CloseWindow(); // Close window and OpenGL context
9598
//--------------------------------------------------------------------------------------

‎examples/others/iqm_loader/riqm.h

+2-737
Large diffs are not rendered by default.

‎src/models.c

+493-159
Large diffs are not rendered by default.

‎src/raylib.h

+21-12
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ typedef enum {
752752
MAP_IRRADIANCE, // NOTE: Uses GL_TEXTURE_CUBE_MAP
753753
MAP_PREFILTER, // NOTE: Uses GL_TEXTURE_CUBE_MAP
754754
MAP_BRDF
755-
} TexmapIndex;
755+
} MaterialMapType;
756756

757757
#define MAP_DIFFUSE MAP_ALBEDO
758758
#define MAP_SPECULAR MAP_METALNESS
@@ -1256,16 +1256,25 @@ RLAPI void DrawGizmo(Vector3 position);
12561256
// Model loading/unloading functions
12571257
RLAPI Model LoadModel(const char *fileName); // Load model from files (meshes and materials)
12581258
RLAPI Model LoadModelFromMesh(Mesh mesh); // Load model from generated mesh
1259-
//RLAPI void LoadModelAnimations(const char fileName, ModelAnimation *anims, int *animsCount); // Load model animations from file
1260-
//RLAPI void UpdateModelAnimation(Model model, ModelAnimation anim, int frame); // Update model animation pose
12611259
RLAPI void UnloadModel(Model model); // Unload model from memory (RAM and/or VRAM)
12621260

1263-
// Mesh manipulation functions
1264-
RLAPI BoundingBox MeshBoundingBox(Mesh mesh); // Compute mesh bounding box limits
1265-
RLAPI void MeshTangents(Mesh *mesh); // Compute mesh tangents
1266-
RLAPI void MeshBinormals(Mesh *mesh); // Compute mesh binormals
1267-
RLAPI void UnloadMesh(Mesh *mesh); // Unload mesh from memory (RAM and/or VRAM)
1261+
// Mesh loading/unloading functions
1262+
RLAPI Mesh *LoadMeshes(const char *fileName, int *meshCount); // Load meshes from model file
12681263
RLAPI void ExportMesh(Mesh mesh, const char *fileName); // Export mesh data to file
1264+
RLAPI void UnloadMesh(Mesh *mesh); // Unload mesh from memory (RAM and/or VRAM)
1265+
1266+
// Material loading/unloading functions
1267+
RLAPI Material *LoadMaterials(const char *fileName, int *materialCount); // Load materials from model file
1268+
RLAPI Material LoadMaterialDefault(void); // Load default material (Supports: DIFFUSE, SPECULAR, NORMAL maps)
1269+
RLAPI void UnloadMaterial(Material material); // Unload material from GPU memory (VRAM)
1270+
RLAPI void SetMaterialTexture(Material *material, int mapType, Texture2D texture); // Set texture for a material map type (MAP_DIFFUSE, MAP_SPECULAR...)
1271+
RLAPI void SetModelMeshMaterial(Model *model, int meshId, int materialId); // Set material for a mesh
1272+
1273+
// Model animations loading/unloading functions
1274+
RLAPI ModelAnimation *LoadModelAnimations(const char *fileName, int *animsCount); // Load model animations from file
1275+
RLAPI void UpdateModelAnimation(Model model, ModelAnimation anim, int frame); // Update model animation pose
1276+
RLAPI void UnloadModelAnimation(ModelAnimation anim); // Unload animation data
1277+
RLAPI bool IsModelAnimationValid(Model model, ModelAnimation anim); // Check model animation skeleton match
12691278

12701279
// Mesh generation functions
12711280
RLAPI Mesh GenMeshPoly(int sides, float radius); // Generate polygonal mesh
@@ -1279,10 +1288,10 @@ RLAPI Mesh GenMeshKnot(float radius, float size, int radSeg, int sides);
12791288
RLAPI Mesh GenMeshHeightmap(Image heightmap, Vector3 size); // Generate heightmap mesh from image data
12801289
RLAPI Mesh GenMeshCubicmap(Image cubicmap, Vector3 cubeSize); // Generate cubes-based map mesh from image data
12811290

1282-
// Material loading/unloading functions
1283-
RLAPI Material LoadMaterial(const char *fileName); // Load material from file
1284-
RLAPI Material LoadMaterialDefault(void); // Load default material (Supports: DIFFUSE, SPECULAR, NORMAL maps)
1285-
RLAPI void UnloadMaterial(Material material); // Unload material from GPU memory (VRAM)
1291+
// Mesh manipulation functions
1292+
RLAPI BoundingBox MeshBoundingBox(Mesh mesh); // Compute mesh bounding box limits
1293+
RLAPI void MeshTangents(Mesh *mesh); // Compute mesh tangents
1294+
RLAPI void MeshBinormals(Mesh *mesh); // Compute mesh binormals
12861295

12871296
// Model drawing functions
12881297
RLAPI void DrawModel(Model model, Vector3 position, float scale, Color tint); // Draw a model (with texture if set)

0 commit comments

Comments
 (0)
Please sign in to comment.