mirror of https://github.com/bjornbytes/lovr.git
rm AnimationData;
This commit is contained in:
parent
e859350e25
commit
4a68067e28
|
@ -246,7 +246,6 @@ set(LOVR_SRC
|
|||
src/api/types/world.c
|
||||
src/audio/audio.c
|
||||
src/audio/source.c
|
||||
src/data/animation.c
|
||||
src/data/audioStream.c
|
||||
src/data/data.c
|
||||
src/data/material.c
|
||||
|
|
|
@ -799,7 +799,7 @@ int l_lovrGraphicsStencil(lua_State* L) {
|
|||
|
||||
int l_lovrGraphicsNewAnimator(lua_State* L) {
|
||||
Model* model = luax_checktype(L, 1, Model);
|
||||
Animator* animator = lovrAnimatorCreate(model->modelData->animationData);
|
||||
Animator* animator = lovrAnimatorCreate(model->modelData);
|
||||
luax_pushtype(L, Animator, animator);
|
||||
return 1;
|
||||
}
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
#include "data/animation.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
AnimationData* lovrAnimationDataCreate() {
|
||||
AnimationData* animationData = malloc(sizeof(AnimationData));
|
||||
if (!animationData) return NULL;
|
||||
|
||||
vec_init(&animationData->animations);
|
||||
|
||||
return animationData;
|
||||
}
|
||||
|
||||
void lovrAnimationDataDestroy(AnimationData* animationData) {
|
||||
vec_deinit(&animationData->animations);
|
||||
free(animationData);
|
||||
}
|
|
@ -1,36 +0,0 @@
|
|||
#include "lib/vec/vec.h"
|
||||
#include "lib/map/map.h"
|
||||
|
||||
#pragma once
|
||||
|
||||
typedef struct {
|
||||
double time;
|
||||
float data[4];
|
||||
} Keyframe;
|
||||
|
||||
typedef vec_t(Keyframe) vec_keyframe_t;
|
||||
|
||||
typedef struct {
|
||||
const char* node;
|
||||
vec_keyframe_t positionKeyframes;
|
||||
vec_keyframe_t rotationKeyframes;
|
||||
vec_keyframe_t scaleKeyframes;
|
||||
} AnimationChannel;
|
||||
|
||||
typedef map_t(AnimationChannel) map_channel_t;
|
||||
|
||||
typedef struct {
|
||||
const char* name;
|
||||
float duration;
|
||||
map_channel_t channels;
|
||||
int channelCount;
|
||||
} Animation;
|
||||
|
||||
typedef vec_t(Animation) vec_animation_t;
|
||||
|
||||
typedef struct {
|
||||
vec_animation_t animations;
|
||||
} AnimationData;
|
||||
|
||||
AnimationData* lovrAnimationDataCreate();
|
||||
void lovrAnimationDataDestroy(AnimationData* animationData);
|
|
@ -395,18 +395,19 @@ ModelData* lovrModelDataCreate(Blob* blob) {
|
|||
assimpNodeTraversal(modelData, scene->mRootNode, &nodeIndex);
|
||||
|
||||
// Animations
|
||||
modelData->animationData = lovrAnimationDataCreate();
|
||||
for (unsigned int i = 0; i < scene->mNumAnimations; i++) {
|
||||
modelData->animationCount = scene->mNumAnimations;
|
||||
modelData->animations = malloc(modelData->animationCount * sizeof(Animation));
|
||||
for (int i = 0; i < modelData->animationCount; i++) {
|
||||
struct aiAnimation* assimpAnimation = scene->mAnimations[i];
|
||||
float ticksPerSecond = assimpAnimation->mTicksPerSecond;
|
||||
|
||||
Animation animation;
|
||||
animation.name = strdup(assimpAnimation->mName.data);
|
||||
animation.duration = assimpAnimation->mDuration / ticksPerSecond;
|
||||
animation.channelCount = assimpAnimation->mNumChannels;
|
||||
map_init(&animation.channels);
|
||||
Animation* animation = &modelData->animations[i];
|
||||
animation->name = strdup(assimpAnimation->mName.data);
|
||||
animation->duration = assimpAnimation->mDuration / ticksPerSecond;
|
||||
animation->channelCount = assimpAnimation->mNumChannels;
|
||||
map_init(&animation->channels);
|
||||
|
||||
for (int j = 0; j < animation.channelCount; j++) {
|
||||
for (int j = 0; j < animation->channelCount; j++) {
|
||||
struct aiNodeAnim* assimpChannel = assimpAnimation->mChannels[j];
|
||||
AnimationChannel channel;
|
||||
|
||||
|
@ -442,10 +443,8 @@ ModelData* lovrModelDataCreate(Blob* blob) {
|
|||
vec_push(&channel.scaleKeyframes, keyframe);
|
||||
}
|
||||
|
||||
map_set(&animation.channels, channel.node, channel);
|
||||
map_set(&animation->channels, channel.node, channel);
|
||||
}
|
||||
|
||||
vec_push(&modelData->animationData->animations, animation);
|
||||
}
|
||||
|
||||
aiReleaseImport(scene);
|
||||
|
@ -464,8 +463,17 @@ void lovrModelDataDestroy(const Ref* ref) {
|
|||
map_deinit(&modelData->primitives[i].boneMap);
|
||||
}
|
||||
|
||||
if (modelData->animationData) {
|
||||
lovrAnimationDataDestroy(modelData->animationData);
|
||||
for (int i = 0; i < modelData->animationCount; i++) {
|
||||
Animation* animation = &modelData->animations[i];
|
||||
const char* key;
|
||||
map_iter_t iter = map_iter(&animation->channels);
|
||||
while ((key = map_next(&animation->channels, &iter)) != NULL) {
|
||||
AnimationChannel* channel = map_get(&animation->channels, key);
|
||||
vec_deinit(&channel->positionKeyframes);
|
||||
vec_deinit(&channel->rotationKeyframes);
|
||||
vec_deinit(&channel->scaleKeyframes);
|
||||
}
|
||||
map_deinit(&animation->channels);
|
||||
}
|
||||
|
||||
for (int i = 0; i < modelData->materialCount; i++) {
|
||||
|
@ -476,6 +484,7 @@ void lovrModelDataDestroy(const Ref* ref) {
|
|||
|
||||
free(modelData->nodes);
|
||||
free(modelData->primitives);
|
||||
free(modelData->animations);
|
||||
free(modelData->materials);
|
||||
free(modelData->vertices.data);
|
||||
free(modelData->indices.data);
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
#include "filesystem/blob.h"
|
||||
#include "data/animation.h"
|
||||
#include "data/material.h"
|
||||
#include "util.h"
|
||||
#include "lib/vertex.h"
|
||||
#include "lib/map/map.h"
|
||||
#include "lib/vec/vec.h"
|
||||
|
||||
#pragma once
|
||||
|
@ -32,12 +32,40 @@ typedef struct ModelNode {
|
|||
vec_uint_t primitives;
|
||||
} ModelNode;
|
||||
|
||||
typedef struct {
|
||||
Color diffuseColor;
|
||||
int diffuseTexture;
|
||||
} ModelMaterial;
|
||||
|
||||
typedef struct {
|
||||
double time;
|
||||
float data[4];
|
||||
} Keyframe;
|
||||
|
||||
typedef vec_t(Keyframe) vec_keyframe_t;
|
||||
|
||||
typedef struct {
|
||||
const char* node;
|
||||
vec_keyframe_t positionKeyframes;
|
||||
vec_keyframe_t rotationKeyframes;
|
||||
vec_keyframe_t scaleKeyframes;
|
||||
} AnimationChannel;
|
||||
|
||||
typedef map_t(AnimationChannel) map_channel_t;
|
||||
|
||||
typedef struct {
|
||||
const char* name;
|
||||
float duration;
|
||||
map_channel_t channels;
|
||||
int channelCount;
|
||||
} Animation;
|
||||
|
||||
typedef struct {
|
||||
Ref ref;
|
||||
ModelNode* nodes;
|
||||
map_int_t nodeMap;
|
||||
ModelPrimitive* primitives;
|
||||
AnimationData* animationData;
|
||||
Animation* animations;
|
||||
MaterialData** materials;
|
||||
VertexFormat format;
|
||||
VertexData vertices;
|
||||
|
|
|
@ -13,17 +13,18 @@ static int trackSortCallback(const void* a, const void* b) {
|
|||
return ((Track*) a)->priority < ((Track*) b)->priority;
|
||||
}
|
||||
|
||||
Animator* lovrAnimatorCreate(AnimationData* animationData) {
|
||||
Animator* lovrAnimatorCreate(ModelData* modelData) {
|
||||
Animator* animator = lovrAlloc(sizeof(Animator), lovrAnimatorDestroy);
|
||||
if (!animator) return NULL;
|
||||
|
||||
animator->animationData = animationData;
|
||||
lovrRetain(&modelData->ref);
|
||||
animator->modelData = modelData;
|
||||
map_init(&animator->trackMap);
|
||||
vec_init(&animator->trackList);
|
||||
animator->speed = 1;
|
||||
|
||||
for (int i = 0; i < animationData->animations.length; i++) {
|
||||
Animation* animation = &animationData->animations.data[i];
|
||||
for (int i = 0; i < modelData->animationCount; i++) {
|
||||
Animation* animation = &modelData->animations[i];
|
||||
|
||||
Track track = {
|
||||
.animation = animation,
|
||||
|
@ -44,6 +45,7 @@ Animator* lovrAnimatorCreate(AnimationData* animationData) {
|
|||
|
||||
void lovrAnimatorDestroy(const Ref* ref) {
|
||||
Animator* animator = containerof(ref, Animator);
|
||||
lovrRelease(&animator->modelData->ref);
|
||||
map_deinit(&animator->trackMap);
|
||||
vec_deinit(&animator->trackList);
|
||||
free(animator);
|
||||
|
@ -195,15 +197,15 @@ bool lovrAnimatorEvaluate(Animator* animator, const char* bone, mat4 transform)
|
|||
}
|
||||
|
||||
int lovrAnimatorGetAnimationCount(Animator* animator) {
|
||||
return animator->animationData->animations.length;
|
||||
return animator->modelData->animationCount;
|
||||
}
|
||||
|
||||
const char* lovrAnimatorGetAnimationName(Animator* animator, int index) {
|
||||
if (index < 0 || index >= animator->animationData->animations.length) {
|
||||
if (index < 0 || index >= animator->modelData->animationCount) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return animator->animationData->animations.data[index].name;
|
||||
return animator->modelData->animations[index].name;
|
||||
}
|
||||
|
||||
void lovrAnimatorPlay(Animator* animator, const char* animation) {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#include "data/animation.h"
|
||||
#include "data/model.h"
|
||||
#include "math/mat4.h"
|
||||
#include "util.h"
|
||||
#include "lib/map/map.h"
|
||||
|
@ -20,13 +20,13 @@ typedef map_t(Track) map_track_t;
|
|||
|
||||
typedef struct {
|
||||
Ref ref;
|
||||
AnimationData* animationData;
|
||||
ModelData* modelData;
|
||||
map_track_t trackMap;
|
||||
vec_void_t trackList;
|
||||
float speed;
|
||||
} Animator;
|
||||
|
||||
Animator* lovrAnimatorCreate(AnimationData* animationData);
|
||||
Animator* lovrAnimatorCreate(ModelData* modelData);
|
||||
void lovrAnimatorDestroy(const Ref* ref);
|
||||
void lovrAnimatorReset(Animator* animator);
|
||||
void lovrAnimatorUpdate(Animator* animator, float dt);
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
#include "data/model.h"
|
||||
#include "data/animation.h"
|
||||
#include "graphics/animator.h"
|
||||
#include "graphics/material.h"
|
||||
#include "graphics/mesh.h"
|
||||
|
|
|
@ -678,8 +678,8 @@ static ModelData* openvrControllerNewModelData(Controller* controller) {
|
|||
|
||||
modelData->nodes = malloc(1 * sizeof(ModelNode));
|
||||
modelData->primitives = malloc(1 * sizeof(ModelPrimitive));
|
||||
modelData->animations = NULL;
|
||||
modelData->materials = malloc(1 * sizeof(MaterialData*));
|
||||
modelData->animationData = NULL;
|
||||
|
||||
// Geometry
|
||||
map_init(&modelData->nodeMap);
|
||||
|
|
Loading…
Reference in New Issue