mirror of https://github.com/bjornbytes/lovr.git
Start Animator;
This commit is contained in:
parent
1f4404a695
commit
ff0d007afe
|
@ -1,5 +1,6 @@
|
||||||
#include "api/lovr.h"
|
#include "api/lovr.h"
|
||||||
#include "graphics/graphics.h"
|
#include "graphics/graphics.h"
|
||||||
|
#include "graphics/animator.h"
|
||||||
#include "graphics/material.h"
|
#include "graphics/material.h"
|
||||||
#include "graphics/mesh.h"
|
#include "graphics/mesh.h"
|
||||||
#include "graphics/model.h"
|
#include "graphics/model.h"
|
||||||
|
@ -73,6 +74,7 @@ static void luax_readvertices(lua_State* L, int index, vec_float_t* points) {
|
||||||
int l_lovrGraphicsInit(lua_State* L) {
|
int l_lovrGraphicsInit(lua_State* L) {
|
||||||
lua_newtable(L);
|
lua_newtable(L);
|
||||||
luaL_register(L, NULL, lovrGraphics);
|
luaL_register(L, NULL, lovrGraphics);
|
||||||
|
luax_registertype(L, "Animator", lovrAnimator);
|
||||||
luax_registertype(L, "Font", lovrFont);
|
luax_registertype(L, "Font", lovrFont);
|
||||||
luax_registertype(L, "Material", lovrMaterial);
|
luax_registertype(L, "Material", lovrMaterial);
|
||||||
luax_registertype(L, "Mesh", lovrMesh);
|
luax_registertype(L, "Mesh", lovrMesh);
|
||||||
|
@ -570,6 +572,13 @@ int l_lovrGraphicsPrint(lua_State* L) {
|
||||||
|
|
||||||
// Types
|
// Types
|
||||||
|
|
||||||
|
int l_lovrGraphicsNewAnimator(lua_State* L) {
|
||||||
|
Model* model = luax_checktype(L, 1, Model);
|
||||||
|
Animator* animator = lovrAnimatorCreate(model->modelData->animationData);
|
||||||
|
luax_pushtype(L, Animator, animator);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
int l_lovrGraphicsNewFont(lua_State* L) {
|
int l_lovrGraphicsNewFont(lua_State* L) {
|
||||||
Blob* blob = NULL;
|
Blob* blob = NULL;
|
||||||
float size;
|
float size;
|
||||||
|
@ -810,6 +819,7 @@ const luaL_Reg lovrGraphics[] = {
|
||||||
{ "sphere", l_lovrGraphicsSphere },
|
{ "sphere", l_lovrGraphicsSphere },
|
||||||
{ "skybox", l_lovrGraphicsSkybox },
|
{ "skybox", l_lovrGraphicsSkybox },
|
||||||
{ "print", l_lovrGraphicsPrint },
|
{ "print", l_lovrGraphicsPrint },
|
||||||
|
{ "newAnimator", l_lovrGraphicsNewAnimator },
|
||||||
{ "newFont", l_lovrGraphicsNewFont },
|
{ "newFont", l_lovrGraphicsNewFont },
|
||||||
{ "newMaterial", l_lovrGraphicsNewMaterial },
|
{ "newMaterial", l_lovrGraphicsNewMaterial },
|
||||||
{ "newMesh", l_lovrGraphicsNewMesh },
|
{ "newMesh", l_lovrGraphicsNewMesh },
|
||||||
|
|
|
@ -15,6 +15,7 @@ int l_lovrMathInit(lua_State* L);
|
||||||
int l_lovrPhysicsInit(lua_State* L);
|
int l_lovrPhysicsInit(lua_State* L);
|
||||||
int l_lovrTimerInit(lua_State* L);
|
int l_lovrTimerInit(lua_State* L);
|
||||||
|
|
||||||
|
extern const luaL_Reg lovrAnimator[];
|
||||||
extern const luaL_Reg lovrAudio[];
|
extern const luaL_Reg lovrAudio[];
|
||||||
extern const luaL_Reg lovrBallJoint[];
|
extern const luaL_Reg lovrBallJoint[];
|
||||||
extern const luaL_Reg lovrBoxShape[];
|
extern const luaL_Reg lovrBoxShape[];
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
#include "api/lovr.h"
|
||||||
|
#include "graphics/animator.h"
|
||||||
|
|
||||||
|
int l_lovrAnimatorGetAnimationCount(lua_State* L) {
|
||||||
|
Animator* animator = luax_checktype(L, 1, Animator);
|
||||||
|
lua_pushnumber(L, lovrAnimatorGetAnimationCount(animator));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const luaL_Reg lovrAnimator[] = {
|
||||||
|
{ "getAnimationCount", l_lovrAnimatorGetAnimationCount },
|
||||||
|
{ NULL, NULL }
|
||||||
|
};
|
|
@ -0,0 +1,19 @@
|
||||||
|
#include "graphics/animator.h"
|
||||||
|
|
||||||
|
Animator* lovrAnimatorCreate(AnimationData* animationData) {
|
||||||
|
Animator* animator = lovrAlloc(sizeof(Animator), lovrAnimatorDestroy);
|
||||||
|
if (!animator) return NULL;
|
||||||
|
|
||||||
|
animator->animationData = animationData;
|
||||||
|
|
||||||
|
return animator;
|
||||||
|
}
|
||||||
|
|
||||||
|
void lovrAnimatorDestroy(const Ref* ref) {
|
||||||
|
Animator* animator = containerof(ref, Animator);
|
||||||
|
free(animator);
|
||||||
|
}
|
||||||
|
|
||||||
|
int lovrAnimatorGetAnimationCount(Animator* animator) {
|
||||||
|
return animator->animationData->animations.length;
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
#include "util.h"
|
||||||
|
#include "loaders/animation.h"
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
Ref ref;
|
||||||
|
AnimationData* animationData;
|
||||||
|
} Animator;
|
||||||
|
|
||||||
|
Animator* lovrAnimatorCreate(AnimationData* animationData);
|
||||||
|
void lovrAnimatorDestroy(const Ref* ref);
|
||||||
|
int lovrAnimatorGetAnimationCount(Animator* animator);
|
|
@ -5,13 +5,12 @@ AnimationData* lovrAnimationDataCreate(const char* name) {
|
||||||
AnimationData* animationData = malloc(sizeof(AnimationData));
|
AnimationData* animationData = malloc(sizeof(AnimationData));
|
||||||
if (!animationData) return NULL;
|
if (!animationData) return NULL;
|
||||||
|
|
||||||
animationData->name = name;
|
vec_init(&animationData->animations);
|
||||||
map_init(&animationData->channels);
|
|
||||||
|
|
||||||
return animationData;
|
return animationData;
|
||||||
}
|
}
|
||||||
|
|
||||||
void lovrAnimationDataDestroy(AnimationData* animationData) {
|
void lovrAnimationDataDestroy(AnimationData* animationData) {
|
||||||
map_deinit(&animationData->channels);
|
vec_deinit(&animationData->animations);
|
||||||
free(animationData);
|
free(animationData);
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,13 @@ typedef struct {
|
||||||
const char* name;
|
const char* name;
|
||||||
map_channel_t channels;
|
map_channel_t channels;
|
||||||
int channelCount;
|
int channelCount;
|
||||||
|
} Animation;
|
||||||
|
|
||||||
|
typedef vec_t(Animation) vec_animation_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
vec_animation_t animations;
|
||||||
} AnimationData;
|
} AnimationData;
|
||||||
|
|
||||||
AnimationData* lovrAnimationDataCreate(const char* name);
|
AnimationData* lovrAnimationDataCreate();
|
||||||
void lovrAnimationDataDestroy(AnimationData* animationData);
|
void lovrAnimationDataDestroy(AnimationData* animationData);
|
||||||
|
|
|
@ -385,14 +385,16 @@ ModelData* lovrModelDataCreate(Blob* blob) {
|
||||||
assimpNodeTraversal(modelData, scene->mRootNode, &nodeIndex);
|
assimpNodeTraversal(modelData, scene->mRootNode, &nodeIndex);
|
||||||
|
|
||||||
// Animations
|
// Animations
|
||||||
modelData->animationCount = scene->mNumAnimations;
|
modelData->animationData = lovrAnimationDataCreate();
|
||||||
modelData->animations = malloc(modelData->animationCount * sizeof(AnimationData*));
|
for (unsigned int i = 0; i < scene->mNumAnimations; i++) {
|
||||||
for (int i = 0; i < modelData->animationCount; i++) {
|
|
||||||
struct aiAnimation* assimpAnimation = scene->mAnimations[i];
|
struct aiAnimation* assimpAnimation = scene->mAnimations[i];
|
||||||
AnimationData* animationData = lovrAnimationDataCreate(assimpAnimation->mName.data);
|
|
||||||
animationData->channelCount = assimpAnimation->mNumChannels;
|
|
||||||
|
|
||||||
for (int j = 0; j < animationData->channelCount; j++) {
|
Animation animation;
|
||||||
|
animation.name = strdup(assimpAnimation->mName.data);
|
||||||
|
animation.channelCount = assimpAnimation->mNumChannels;
|
||||||
|
map_init(&animation.channels);
|
||||||
|
|
||||||
|
for (int j = 0; j < animation.channelCount; j++) {
|
||||||
struct aiNodeAnim* assimpChannel = assimpAnimation->mChannels[j];
|
struct aiNodeAnim* assimpChannel = assimpAnimation->mChannels[j];
|
||||||
AnimationChannel channel;
|
AnimationChannel channel;
|
||||||
|
|
||||||
|
@ -428,10 +430,10 @@ ModelData* lovrModelDataCreate(Blob* blob) {
|
||||||
vec_push(&channel.scaleKeyframes, keyframe);
|
vec_push(&channel.scaleKeyframes, keyframe);
|
||||||
}
|
}
|
||||||
|
|
||||||
map_set(&animationData->channels, channel.node, channel);
|
map_set(&animation.channels, channel.node, channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
modelData->animations[i] = animationData;
|
vec_push(&modelData->animationData->animations, animation);
|
||||||
}
|
}
|
||||||
|
|
||||||
aiReleaseImport(scene);
|
aiReleaseImport(scene);
|
||||||
|
@ -444,9 +446,7 @@ void lovrModelDataDestroy(ModelData* modelData) {
|
||||||
vec_deinit(&modelData->nodes[i].primitives);
|
vec_deinit(&modelData->nodes[i].primitives);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < modelData->animationCount; i++) {
|
lovrAnimationDataDestroy(modelData->animationData);
|
||||||
lovrAnimationDataDestroy(modelData->animations[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < modelData->materialCount; i++) {
|
for (int i = 0; i < modelData->materialCount; i++) {
|
||||||
lovrMaterialDataDestroy(modelData->materials[i]);
|
lovrMaterialDataDestroy(modelData->materials[i]);
|
||||||
|
|
|
@ -47,7 +47,7 @@ typedef struct {
|
||||||
ModelPrimitive* primitives;
|
ModelPrimitive* primitives;
|
||||||
vec_bone_t bones;
|
vec_bone_t bones;
|
||||||
map_int_t boneMap;
|
map_int_t boneMap;
|
||||||
AnimationData** animations;
|
AnimationData* animationData;
|
||||||
MaterialData** materials;
|
MaterialData** materials;
|
||||||
ModelVertices vertices;
|
ModelVertices vertices;
|
||||||
ModelIndices indices;
|
ModelIndices indices;
|
||||||
|
|
Loading…
Reference in New Issue