first cut at migrating entity animation frame calculations to use AnimationLoop

This commit is contained in:
ZappoMan 2014-11-12 16:10:35 -08:00
parent d2ab3e69f0
commit 245f019836
3 changed files with 43 additions and 24 deletions

View file

@ -40,6 +40,18 @@
} \ } \
} }
#define READ_ENTITY_PROPERTY_SETTER(P,T,M) \
if (propertyFlags.getHasProperty(P)) { \
T fromBuffer; \
memcpy(&fromBuffer, dataAt, sizeof(fromBuffer)); \
dataAt += sizeof(fromBuffer); \
bytesRead += sizeof(fromBuffer); \
if (overwriteLocalData) { \
M(fromBuffer); \
} \
}
#define READ_ENTITY_PROPERTY_QUAT(P,M) \ #define READ_ENTITY_PROPERTY_QUAT(P,M) \
if (propertyFlags.getHasProperty(P)) { \ if (propertyFlags.getHasProperty(P)) { \
glm::quat fromBuffer; \ glm::quat fromBuffer; \

View file

@ -33,10 +33,11 @@ ModelEntityItem::ModelEntityItem(const EntityItemID& entityItemID, const EntityI
{ {
_type = EntityTypes::Model; _type = EntityTypes::Model;
setProperties(properties, true); setProperties(properties, true);
_animationFrameIndex = 0.0f;
_lastAnimated = usecTimestampNow(); _lastAnimated = usecTimestampNow();
_jointMappingCompleted = false; _jointMappingCompleted = false;
_color[0] = _color[1] = _color[2] = 0; _color[0] = _color[1] = _color[2] = 0;
//_animationFrameIndex = 0.0f;
} }
EntityItemProperties ModelEntityItem::getProperties() const { EntityItemProperties ModelEntityItem::getProperties() const {
@ -100,9 +101,9 @@ int ModelEntityItem::readEntitySubclassDataFromBuffer(const unsigned char* data,
READ_ENTITY_PROPERTY_COLOR(PROP_COLOR, _color); READ_ENTITY_PROPERTY_COLOR(PROP_COLOR, _color);
READ_ENTITY_PROPERTY_STRING(PROP_MODEL_URL, setModelURL); READ_ENTITY_PROPERTY_STRING(PROP_MODEL_URL, setModelURL);
READ_ENTITY_PROPERTY_STRING(PROP_ANIMATION_URL, setAnimationURL); READ_ENTITY_PROPERTY_STRING(PROP_ANIMATION_URL, setAnimationURL);
READ_ENTITY_PROPERTY(PROP_ANIMATION_FPS, float, _animationFPS); READ_ENTITY_PROPERTY_SETTER(PROP_ANIMATION_FPS, float, setAnimationFPS);
READ_ENTITY_PROPERTY(PROP_ANIMATION_FRAME_INDEX, float, _animationFrameIndex); READ_ENTITY_PROPERTY_SETTER(PROP_ANIMATION_FRAME_INDEX, float, setAnimationFrameIndex);
READ_ENTITY_PROPERTY(PROP_ANIMATION_PLAYING, bool, _animationIsPlaying); READ_ENTITY_PROPERTY_SETTER(PROP_ANIMATION_PLAYING, bool, setAnimationIsPlaying);
READ_ENTITY_PROPERTY_STRING(PROP_TEXTURES, setTextures); READ_ENTITY_PROPERTY_STRING(PROP_TEXTURES, setTextures);
return bytesRead; return bytesRead;
@ -199,19 +200,25 @@ int ModelEntityItem::oldVersionReadEntityDataFromBuffer(const unsigned char* dat
bytesRead += animationURLLength; bytesRead += animationURLLength;
// animationIsPlaying // animationIsPlaying
memcpy(&_animationIsPlaying, dataAt, sizeof(_animationIsPlaying)); bool animationIsPlaying;
dataAt += sizeof(_animationIsPlaying); memcpy(&animationIsPlaying, dataAt, sizeof(animationIsPlaying));
bytesRead += sizeof(_animationIsPlaying); dataAt += sizeof(animationIsPlaying);
bytesRead += sizeof(animationIsPlaying);
setAnimationIsPlaying(animationIsPlaying);
// animationFrameIndex // animationFrameIndex
memcpy(&_animationFrameIndex, dataAt, sizeof(_animationFrameIndex)); float animationFrameIndex;
dataAt += sizeof(_animationFrameIndex); memcpy(&animationFrameIndex, dataAt, sizeof(animationFrameIndex));
bytesRead += sizeof(_animationFrameIndex); dataAt += sizeof(animationFrameIndex);
bytesRead += sizeof(animationFrameIndex);
setAnimationFrameIndex(animationFrameIndex);
// animationFPS // animationFPS
memcpy(&_animationFPS, dataAt, sizeof(_animationFPS)); float animationFPS;
dataAt += sizeof(_animationFPS); memcpy(&animationFPS, dataAt, sizeof(animationFPS));
bytesRead += sizeof(_animationFPS); dataAt += sizeof(animationFPS);
bytesRead += sizeof(animationFPS);
setAnimationFPS(animationFPS);
} }
} }
return bytesRead; return bytesRead;
@ -314,7 +321,7 @@ QVector<glm::quat> ModelEntityItem::getAnimationFrame() {
int frameCount = frames.size(); int frameCount = frames.size();
if (frameCount > 0) { if (frameCount > 0) {
int animationFrameIndex = (int)(glm::floor(_animationFrameIndex)) % frameCount; int animationFrameIndex = (int)(glm::floor(getAnimationFrameIndex())) % frameCount;
if (animationFrameIndex < 0 || animationFrameIndex > frameCount) { if (animationFrameIndex < 0 || animationFrameIndex > frameCount) {
animationFrameIndex = 0; animationFrameIndex = 0;
@ -363,7 +370,7 @@ void ModelEntityItem::update(const quint64& updateTime) {
if (getAnimationIsPlaying()) { if (getAnimationIsPlaying()) {
float deltaTime = (float)(now - _lastAnimated) / (float)USECS_PER_SECOND; float deltaTime = (float)(now - _lastAnimated) / (float)USECS_PER_SECOND;
_lastAnimated = now; _lastAnimated = now;
_animationFrameIndex += deltaTime * _animationFPS; _animationLoop.simulate(deltaTime);
} else { } else {
_lastAnimated = now; _lastAnimated = now;
} }

View file

@ -12,6 +12,8 @@
#ifndef hifi_ModelEntityItem_h #ifndef hifi_ModelEntityItem_h
#define hifi_ModelEntityItem_h #define hifi_ModelEntityItem_h
#include <AnimationLoop.h>
#include "EntityItem.h" #include "EntityItem.h"
class ModelEntityItem : public EntityItem { class ModelEntityItem : public EntityItem {
@ -73,21 +75,21 @@ public:
void setModelURL(const QString& url) { _modelURL = url; } void setModelURL(const QString& url) { _modelURL = url; }
void setAnimationURL(const QString& url) { _animationURL = url; } void setAnimationURL(const QString& url) { _animationURL = url; }
static const float DEFAULT_ANIMATION_FRAME_INDEX; static const float DEFAULT_ANIMATION_FRAME_INDEX;
void setAnimationFrameIndex(float value) { _animationFrameIndex = value; } void setAnimationFrameIndex(float value) { _animationLoop.setFrameIndex(value); }
static const bool DEFAULT_ANIMATION_IS_PLAYING; static const bool DEFAULT_ANIMATION_IS_PLAYING;
void setAnimationIsPlaying(bool value) { _animationIsPlaying = value; } void setAnimationIsPlaying(bool value) { _animationLoop.setRunning(value); }
static const float DEFAULT_ANIMATION_FPS; static const float DEFAULT_ANIMATION_FPS;
void setAnimationFPS(float value) { _animationFPS = value; } void setAnimationFPS(float value) { _animationLoop.setFPS(value); }
void mapJoints(const QStringList& modelJointNames); void mapJoints(const QStringList& modelJointNames);
QVector<glm::quat> getAnimationFrame(); QVector<glm::quat> getAnimationFrame();
bool jointsMapped() const { return _jointMappingCompleted; } bool jointsMapped() const { return _jointMappingCompleted; }
bool getAnimationIsPlaying() const { return _animationIsPlaying; } bool getAnimationIsPlaying() const { return _animationLoop.isRunning(); }
float getAnimationFrameIndex() const { return _animationFrameIndex; } float getAnimationFrameIndex() const { return _animationLoop.getFrameIndex(); }
float getAnimationFPS() const { return _animationFPS; } float getAnimationFPS() const { return _animationLoop.getFPS(); }
static const QString DEFAULT_TEXTURES; static const QString DEFAULT_TEXTURES;
const QString& getTextures() const { return _textures; } const QString& getTextures() const { return _textures; }
@ -106,9 +108,7 @@ protected:
quint64 _lastAnimated; quint64 _lastAnimated;
QString _animationURL; QString _animationURL;
float _animationFrameIndex; // we keep this as a float and round to int only when we need the exact index AnimationLoop _animationLoop;
bool _animationIsPlaying;
float _animationFPS;
QString _textures; QString _textures;
// used on client side // used on client side