Load joints in gltf models

This commit is contained in:
Tyler Bezera 2020-02-29 00:44:24 -08:00
parent aef330dddf
commit ada82e546c
3 changed files with 165 additions and 0 deletions

View File

@ -0,0 +1,104 @@
/*******************************************************************************************
*
* raylib [models] example - Load 3d model with animations and play them
*
* This example has been created using raylib 2.5 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Example contributed by Culacant (@culacant) and reviewed by Ramon Santamaria (@raysan5)
*
* Copyright (c) 2019 Culacant (@culacant) and Ramon Santamaria (@raysan5)
*
********************************************************************************************
*
* To export a model from blender, make sure it is not posed, the vertices need to be in the
* same position as they would be in edit mode.
* and that the scale of your models is set to 0. Scaling can be done from the export menu.
*
********************************************************************************************/
#include <stdlib.h>
#include "raylib.h"
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [models] example - model animation");
// Define the camera to look into our 3d world
Camera camera = { 0 };
camera.position = (Vector3){ 10.0f, 10.0f, 10.0f }; // Camera position
camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
camera.fovy = 45.0f; // Camera field-of-view Y
camera.type = CAMERA_PERSPECTIVE; // Camera mode type
Model model2 = LoadModel("resources/models/RiggedFigure.glb");
Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
// Load animation data
int animsCount = 0;
ModelAnimation *anims = LoadModelAnimations("resources/guy/guyanim.iqm", &animsCount);
int animFrameCounter = 0;
SetCameraMode(camera, CAMERA_FREE); // Set free camera mode
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
UpdateCamera(&camera);
// Play animation when spacebar is held down
if (IsKeyDown(KEY_SPACE))
{
animFrameCounter++;
//UpdateModelAnimation(model, anims[0], animFrameCounter);
if (animFrameCounter >= anims[0].frameCount) animFrameCounter = 0;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode3D(camera);
DrawModelEx(model2, position, (Vector3){ 1.0f, 0.0f, 0.0f }, -90.0f, (Vector3){ 1.0f, 1.0f, 1.0f }, WHITE);
for (int i = 0; i < model2.boneCount; i++)
{
//DrawCube(anims[0].framePoses[animFrameCounter][i].translation, 0.2f, 0.2f, 0.2f, RED);
}
DrawGrid(10, 1.0f); // Draw a grid
EndMode3D();
DrawText("PRESS SPACE to PLAY GLTF MODEL ANIMATION", 10, 10, 20, MAROON);
EndDrawing();
//----------------------------------------------------------------------------------
}
// Unload model animations data
for (int i = 0; i < animsCount; i++) UnloadModelAnimation(anims[i]);
RL_FREE(anims);
UnloadModel(model2); // Unload model
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}

Binary file not shown.

View File

@ -3462,12 +3462,14 @@ static Model LoadGLTF(const char *fileName)
- Supports embedded (base64) or external textures - Supports embedded (base64) or external textures
- Loads all raylib supported material textures, values and colors - Loads all raylib supported material textures, values and colors
- Supports multiple mesh per model and multiple primitives per model - Supports multiple mesh per model and multiple primitives per model
- Loading joints for a single skin
Some restrictions (not exhaustive): Some restrictions (not exhaustive):
- Triangle-only meshes - Triangle-only meshes
- Not supported node hierarchies or transforms - Not supported node hierarchies or transforms
- Only supports unsigned short indices (no byte/unsigned int) - Only supports unsigned short indices (no byte/unsigned int)
- Only supports float for texture coordinates (no byte/unsigned short) - Only supports float for texture coordinates (no byte/unsigned short)
- Multiple skins (armatures)
*************************************************************************************/ *************************************************************************************/
@ -3529,6 +3531,63 @@ static Model LoadGLTF(const char *fileName)
for (int i = 0; i < model.meshCount; i++) model.meshes[i].vboId = (unsigned int *)RL_CALLOC(MAX_MESH_VBO, sizeof(unsigned int)); for (int i = 0; i < model.meshCount; i++) model.meshes[i].vboId = (unsigned int *)RL_CALLOC(MAX_MESH_VBO, sizeof(unsigned int));
// Currenly only support one skin
model.boneCount = data->skins[0].joints_count;
model.bones = RL_CALLOC(model.boneCount, sizeof(BoneInfo));
model.bindPose = RL_CALLOC(model.boneCount, sizeof(Transform));
for (int i = 0; i < model.boneCount; i++)
{
if (data->skins[0].joints[i]->name) {
// Raylib only supports names up to 34 characters..
if (strlen(data->skins[0].joints[i]->name) <= 34) {
strcpy(model.bones[i].name, data->skins[0].joints[i]->name);
}
}
// Mark parents in array
if (data->skins[0].joints[i]->children_count > 0) {
int numOfChildrenJoints = data->skins[0].joints[i]->children_count;
for (int j = i; j < numOfChildrenJoints; j++) {
model.bones[j].parent = i;
}
}
// Set the tranlation for this joint
if (data->skins[0].joints[i]->has_translation) {
model.bindPose[i].translation.x = data->skins[0].joints[i]->translation[0];
model.bindPose[i].translation.y = data->skins[0].joints[i]->translation[1];
model.bindPose[i].translation.z = data->skins[0].joints[i]->translation[2];
}
// Set the rotation for this joint
if (data->skins[0].joints[i]->has_rotation) {
model.bindPose[i].rotation.x = data->skins[0].joints[i]->rotation[0];
model.bindPose[i].rotation.y = data->skins[0].joints[i]->rotation[1];
model.bindPose[i].rotation.z = data->skins[0].joints[i]->rotation[2];
model.bindPose[i].rotation.w = data->skins[0].joints[i]->rotation[3];
}
// Set the scale for this joint
if (data->skins[0].joints[i]->has_scale) {
model.bindPose[i].scale.x = data->skins[0].joints[i]->scale[0];
model.bindPose[i].scale.y = data->skins[0].joints[i]->scale[1];
model.bindPose[i].scale.z = data->skins[0].joints[i]->scale[2];
}
}
// Build bind pose from parent joints... Same IQM Loader
for (int i = 0; i < model.boneCount; i++)
{
if (model.bones[i].parent >= 0)
{
model.bindPose[i].rotation = QuaternionMultiply(model.bindPose[model.bones[i].parent].rotation, model.bindPose[i].rotation);
model.bindPose[i].translation = Vector3RotateByQuaternion(model.bindPose[i].translation, model.bindPose[model.bones[i].parent].rotation);
model.bindPose[i].translation = Vector3Add(model.bindPose[i].translation, model.bindPose[model.bones[i].parent].translation);
model.bindPose[i].scale = Vector3Multiply(model.bindPose[i].scale, model.bindPose[model.bones[i].parent].scale);
}
}
TRACELOG(LOG_INFO, "[%s][%d] Joints loaded", fileName, data->skins[0].joints_count);
for (int i = 0; i < model.materialCount - 1; i++) for (int i = 0; i < model.materialCount - 1; i++)
{ {
model.materials[i] = LoadMaterialDefault(); model.materials[i] = LoadMaterialDefault();
@ -3613,6 +3672,8 @@ static Model LoadGLTF(const char *fileName)
cgltf_accessor *acc = data->meshes[i].primitives[p].attributes[j].data; cgltf_accessor *acc = data->meshes[i].primitives[p].attributes[j].data;
model.meshes[primitiveIndex].vertexCount = acc->count; model.meshes[primitiveIndex].vertexCount = acc->count;
model.meshes[primitiveIndex].vertices = RL_MALLOC(sizeof(float)*model.meshes[primitiveIndex].vertexCount*3); model.meshes[primitiveIndex].vertices = RL_MALLOC(sizeof(float)*model.meshes[primitiveIndex].vertexCount*3);
model.meshes[i].animVertices = RL_CALLOC(model.meshes[i].vertexCount*3, sizeof(float));
model.meshes[i].animNormals = RL_CALLOC(model.meshes[i].vertexCount*3, sizeof(float));
LOAD_ACCESSOR(float, 3, acc, model.meshes[primitiveIndex].vertices) LOAD_ACCESSOR(float, 3, acc, model.meshes[primitiveIndex].vertices)
} }