initialize the _lastAnimated time when the modelEntityRenderer is instantiated, this fixes the sync between interface and the server

This commit is contained in:
amantley 2017-11-28 11:04:48 -08:00
parent 522dc3dfb1
commit d607ca0914
4 changed files with 190 additions and 110 deletions

View file

@ -63,13 +63,17 @@ bool ModelEntityWrapper::isModelLoaded() const {
EntityItemPointer RenderableModelEntityItem::factory(const EntityItemID& entityID, const EntityItemProperties& properties) {
EntityItemPointer entity(new RenderableModelEntityItem(entityID, properties.getDimensionsInitialized()),
[](EntityItem* ptr) { ptr->deleteLater(); });
entity->setProperties(properties);
return entity;
}
RenderableModelEntityItem::RenderableModelEntityItem(const EntityItemID& entityItemID, bool dimensionsInitialized) :
ModelEntityWrapper(entityItemID),
_dimensionsInitialized(dimensionsInitialized) {
}
RenderableModelEntityItem::~RenderableModelEntityItem() { }
@ -987,29 +991,17 @@ void ModelEntityRenderer::animate(const TypedEntityPointer& entity) {
}
//get entity model anim props
bool isLooping = entity->getAnimationLoop();
int firstFrame = entity->getAnimationFirstFrame();
int lastFrame = entity->getAnimationLastFrame();
bool isHolding = entity->getAnimationHold();
int updatedFrameCount = lastFrame - firstFrame + 1;
//get the updated frame from the ModelEntity
auto modelAnimProperties = entity->getAnimationProperties();
int updatedFrameCount = entity->getAnimationLastFrame() - entity->getAnimationFirstFrame() + 1;
//_currentFrame = modelAnimProperties.getCurrentFrame();
//tempbool = modelAnimProperties.getRunning();
//qCDebug(entitiesrenderer) << "is playing is: " << tempbool;
qCDebug(entitiesrenderer) << "the client frame count is the following " << _currentFrame;
if ((firstFrame >= 0) && (firstFrame < lastFrame) && (lastFrame <= frameCount)) {
//length of animation in now determined by first and last frame
updatedFrameCount = (lastFrame - firstFrame + 1);
if ((entity->getAnimationFirstFrame() < 0) && (entity->getAnimationFirstFrame() > entity->getAnimationLastFrame())){// && (lastFrame <= frameCount)) {
//we don't increment currentframe if the first frame is < zero or > than last frame.
//return;
}
if (!_lastAnimated) {
_lastAnimated = usecTimestampNow();
return;
@ -1022,45 +1014,37 @@ void ModelEntityRenderer::animate(const TypedEntityPointer& entity) {
auto interval = now - _lastAnimated;
_lastAnimated = now;
float oldCurrentFrame = _currentFrame;
float deltaTime = (float)interval / (float)USECS_PER_SECOND;
_currentFrame += (deltaTime * _renderAnimationProperties.getFPS());
//here we implement the looping animation property
//if we have played through the animation once then we hold on the last frame
if( isLooping || (_currentFrame < _renderAnimationProperties.getLastFrame() ) ){
//else advance the current frame.
//if hold or not playing don't advance the current frame.
//also if the animFrame is outside of first or last frame then don't advance the motion.
if (!isHolding && entity->getAnimationIsPlaying() && !( _renderAnimationProperties.getCurrentFrame() > _renderAnimationProperties.getLastFrame() ) && !( _renderAnimationProperties.getCurrentFrame() < _renderAnimationProperties.getFirstFrame() ) ) {
float deltaTime = (float)interval / (float)USECS_PER_SECOND;
_currentFrame += (deltaTime * _renderAnimationProperties.getFPS());
while ((_currentFrame - _renderAnimationProperties.getFirstFrame()) > updatedFrameCount) {
_currentFrame -= updatedFrameCount;
if (!(entity->getAnimationHold()) && entity->getAnimationIsPlaying()) {
float deltaTime = (float)interval / (float)USECS_PER_SECOND;
_currentFrame += (deltaTime * entity->getAnimationFPS());
if (_currentFrame > entity->getAnimationLastFrame()) {
if (entity->getAnimationLoop()) {
while ((_currentFrame - entity->getAnimationFirstFrame()) > (updatedFrameCount - 1)) {
_currentFrame -= (updatedFrameCount - 1);
}
}else{
_currentFrame = entity->getAnimationLastFrame();
}
}else if (_currentFrame < entity->getAnimationFirstFrame()) {
if (entity->getAnimationFirstFrame() < 0) {
_currentFrame = 0;
}else {
_currentFrame = entity->getAnimationFirstFrame();
}
}
else {
//use old currentFrame
_currentFrame = oldCurrentFrame;
}
}else {
//make current frame the endanim frame
_currentFrame = _renderAnimationProperties.getLastFrame();
}
//save the last place that we rendered ourselves.
//entity->setAnimationCurrentFrame(_currentFrame);
qCDebug(entitiesrenderer) << "_currentFrame " << _currentFrame;
{
{
//where are we in the currently defined animation segment?
// int animationCurrentFrame = (int)(glm::floor(_currentFrame - firstFrame)) % updatedFrameCount;
//this gives us the absolute frame value to use by adding the first frame value.
// animationCurrentFrame += firstFrame;
int animationCurrentFrame = (int)(glm::floor(_currentFrame));
//in the case where the last frame is greater than the framecount then clamp
//it to the end of the animation until it loops around.
if (animationCurrentFrame < 0 || animationCurrentFrame > frameCount) {
animationCurrentFrame = 0;
@ -1382,23 +1366,65 @@ void ModelEntityRenderer::doRenderUpdateSynchronousTyped(const ScenePointer& sce
// make a copy of the animation properites
auto newAnimationProperties = entity->getAnimationProperties();
if (newAnimationProperties != _renderAnimationProperties) {
qCDebug(entitiesrenderer) << "this is where the change is currently handled in the rendering code";
qCDebug(entitiesrenderer) << "getting the currently playing frame from the modelentityitem update" << newAnimationProperties.getCurrentFrame();
withWriteLock([&] {
if ( (newAnimationProperties.getCurrentFrame() != _renderAnimationProperties.getCurrentFrame()) || (newAnimationProperties.getFirstFrame() != _renderAnimationProperties.getFirstFrame()) || (newAnimationProperties.getLastFrame() != _renderAnimationProperties.getLastFrame()) || (newAnimationProperties.getRunning() && !_renderAnimationProperties.getRunning())) {
if (!(newAnimationProperties.getCurrentFrame() > newAnimationProperties.getLastFrame()) && !(newAnimationProperties.getCurrentFrame() < newAnimationProperties.getFirstFrame())) {
if ((newAnimationProperties.getFirstFrame() != _renderAnimationProperties.getFirstFrame()) || (newAnimationProperties.getLastFrame() != _renderAnimationProperties.getLastFrame()) || (newAnimationProperties.getRunning() && !_renderAnimationProperties.getRunning())) {
if (_currentFrame < 0) {
qCDebug(entitiesrenderer) << "point A before assign" << _currentFrame;
_currentFrame = newAnimationProperties.getCurrentFrame();// +((float)newAnimationProperties.getCurrentlyPlayingFrame() / (float)USECS_PER_SECOND)*(newAnimationProperties.getFPS());
_endAnim = _currentFrame + ( newAnimationProperties.getLastFrame() - newAnimationProperties.getFirstFrame() );
_lastAnimated = 0;
qCDebug(entitiesrenderer) << "point A after assign" << _currentFrame;
qCDebug(entitiesrenderer) << "current " <<newAnimationProperties.getCurrentFrame()<< " first "<< newAnimationProperties.getFirstFrame()<< " last " << newAnimationProperties.getLastFrame() <<" running "<< newAnimationProperties.getRunning();
qCDebug(entitiesrenderer) << "The previous current Frame " << newAnimationProperties.getCurrentFrame() << " or " << _renderAnimationProperties.getCurrentFrame();
qCDebug(entitiesrenderer) << "last animated before resume animating" << _lastAnimated;
//_lastAnimated = usecTimestampNow();
//qCDebug(entitiesrenderer) << "last animated" << _lastAnimated;
//tempbool = true;
}else {
_currentFrame = newAnimationProperties.getFirstFrame();
_lastAnimated = usecTimestampNow();
qCDebug(entitiesrenderer) << "point A.2 reset start";
}
}else if ( _renderAnimationProperties.getLoop() && !newAnimationProperties.getLoop()) {
//int currentframe_mod_length = (int)(_currentFrame - (int)(glm::floor(newAnimationProperties.getCurrentFrame()))) % ((int)(glm::floor(newAnimationProperties.getLastFrame())) - (int)(glm::floor(newAnimationProperties.getFirstFrame())) + 1);
//_endAnim = _currentFrame + ((int)(newAnimationProperties.getLastFrame()) - (int)(newAnimationProperties.getFirstFrame())) - (float)currentframe_mod_length;
}else if ( !_renderAnimationProperties.getLoop() && newAnimationProperties.getLoop()) {
//_currentFrame = newAnimationProperties.getCurrentFrame();
qCDebug(entitiesrenderer) << "turn on loop " << _currentFrame;
}else if (_renderAnimationProperties.getLoop() && !newAnimationProperties.getLoop()) {
//_currentFrame = newAnimationProperties.getCurrentFrame();
qCDebug(entitiesrenderer) << "turn off loop " << _currentFrame;
}else if (_renderAnimationProperties.getHold() != newAnimationProperties.getHold()) {
//_currentFrame = newAnimationProperties.getCurrentFrame();
qCDebug(entitiesrenderer) << "get hold " << _currentFrame;
//_lastAnimated = 0;
}else if (_renderAnimationProperties.getCurrentFrame() != newAnimationProperties.getCurrentFrame()){
_currentFrame = newAnimationProperties.getCurrentFrame();
//_lastAnimated = usecTimestampNow();
qCDebug(entitiesrenderer) << "point C " << _currentFrame;
}else {
//do nothing
}
_currentFrame = newAnimationProperties.getCurrentFrame();
qCDebug(entitiesrenderer) << "renderable update to current frame " << _currentFrame;
_renderAnimationProperties = newAnimationProperties;
});
_renderAnimationProperties = entity->getAnimationProperties();
} else {
//if first frame is less than zero then don't do anything.
if (!(entity->getAnimationFirstFrame() < 0)) {
// if the current frame is less than zero, this is from the initialization.
if (_currentFrame < 0) {
qCDebug(entitiesrenderer) << "point D property current frame " << entity->getName() << newAnimationProperties.getCurrentFrame();
if ((newAnimationProperties.getCurrentFrame() < newAnimationProperties.getLastFrame()) && (newAnimationProperties.getCurrentFrame() > newAnimationProperties.getFirstFrame())) {
_currentFrame = newAnimationProperties.getCurrentFrame();
qCDebug(entitiesrenderer) << "point D.1 " << _currentFrame;
qCDebug(entitiesrenderer) << "last animated " << _lastAnimated;
_lastAnimated = usecTimestampNow();
}
else {
_currentFrame = newAnimationProperties.getFirstFrame();
qCDebug(entitiesrenderer) << "point D.2 " << _currentFrame;
_lastAnimated = usecTimestampNow();
qCDebug(entitiesrenderer) << entity->getName() << "last animated " << _lastAnimated;
}
}
}
}
}
//angus
@ -1410,9 +1436,12 @@ void ModelEntityRenderer::doRenderUpdateSynchronousTyped(const ScenePointer& sce
if (!jointsMapped()) {
mapJoints(entity, model->getJointNames());
}
animate(entity);
if (!(entity->getAnimationFirstFrame() < 0) && !(entity->getAnimationFirstFrame() > entity->getAnimationLastFrame())) {
animate(entity);
}
emit requestRenderUpdate();
}
}
void ModelEntityRenderer::flagForCollisionGeometryUpdate() {

View file

@ -133,7 +133,10 @@ class ModelEntityRenderer : public TypedEntityRenderer<RenderableModelEntityItem
friend class EntityRenderer;
public:
ModelEntityRenderer(const EntityItemPointer& entity) : Parent(entity) { }
ModelEntityRenderer(const EntityItemPointer& entity) : Parent(entity) {
_lastAnimated = usecTimestampNow();
qCDebug(entitiesrenderer) << "set the last animated here" << _lastAnimated;
}
protected:
virtual void removeFromScene(const ScenePointer& scene, Transaction& transaction) override;
@ -184,7 +187,7 @@ private:
bool _shouldHighlight { false };
bool _animating { false };
uint64_t _lastAnimated { 0 };
float _currentFrame { 0 };
float _currentFrame { -1 };
float _endAnim{ 0 };
bool tempbool{ false };
quint64 _currentlyPlayingFrame{ 0 };

View file

@ -197,33 +197,67 @@ void ModelEntityItem::update(const quint64& now) {
auto currentAnimationProperties = this->getAnimationProperties();
if (_previousAnimationProperties != currentAnimationProperties) {
qCDebug(entities) << "this is where the _currentFrame change is handled in the ModelEntityItem.cpp code";
//qCDebug(entities) << "properties changed in modelentity code" << _currentFrame;
withWriteLock([&] {
_previousAnimationProperties = currentAnimationProperties;
if ( (currentAnimationProperties.getFirstFrame() != _previousAnimationProperties.getFirstFrame()) || (currentAnimationProperties.getLastFrame() != _previousAnimationProperties.getLastFrame()) || (currentAnimationProperties.getRunning() && !_previousAnimationProperties.getRunning())) {
// if (!(currentAnimationProperties.getCurrentFrame() > currentAnimationProperties.getLastFrame()) && !(currentAnimationProperties.getCurrentFrame() < currentAnimationProperties.getFirstFrame())) {
// _currentlyPlayingFrame = currentAnimationProperties.getCurrentFrame();
//_endAnim = _currentlyPlayingFrame + ( currentAnimationProperties.getLastFrame() - currentAnimationProperties.getFirstFrame() );
//_lastAnimated = 0;
// }
qCDebug(entities) << "this is where the _currentFrame change is handled in the ModelEntityItem.cpp code, current frame is: \n\n" << currentAnimationProperties.getCurrentFrame();
_lastAnimated = usecTimestampNow();
_currentFrame = currentAnimationProperties.getFirstFrame();
qCDebug(entities) << "point 2 " << _currentFrame;
setAnimationCurrentFrame(currentAnimationProperties.getFirstFrame());
}
else {
//else if ( _previousAnimationProperties.getLoop() && !currentAnimationProperties.getLoop()) {
// int currentframe_mod_length = (int)(_currentlyPlayingFrame - (int)(glm::floor(currentAnimationProperties.getCurrentFrame()))) % ((int)(glm::floor(currentAnimationProperties.getLastFrame())) - (int)(glm::floor(currentAnimationProperties.getFirstFrame())) + 1);
//_endAnim = _currentlyPlayingFrame + ((int)(currentAnimationProperties.getLastFrame()) - (int)(currentAnimationProperties.getFirstFrame())) - (float)currentframe_mod_length;
//}
setAnimationCurrentFrame(currentAnimationProperties.getCurrentFrame());
}else if (currentAnimationProperties.getHold() && !_previousAnimationProperties.getHold()) {
//_lastAnimated = 0;
//_currentFrame = currentAnimationProperties.getCurrentFrame();
qCDebug(entities) << "hold is pressed" << _currentFrame;
}else if (!currentAnimationProperties.getHold() && _previousAnimationProperties.getHold()) {
//_lastAnimated = 0;
//_currentFrame = currentAnimationProperties.getCurrentFrame();
qCDebug(entities) << "hold is unpressed" << _currentFrame;
}else if (!currentAnimationProperties.getLoop() && _previousAnimationProperties.getLoop()) {
//_lastAnimated = 0;
qCDebug(entities) << "loop is unpressed" << _currentFrame;
}else if (currentAnimationProperties.getLoop() && !_previousAnimationProperties.getLoop()) {
//_lastAnimated = 0;
qCDebug(entities) << "loop is pressed" << _currentFrame;
}else if(currentAnimationProperties.getCurrentFrame() != _previousAnimationProperties.getCurrentFrame()){
_currentFrame = currentAnimationProperties.getCurrentFrame();
// if (_currentFrame < currentAnimationProperties.getFirstFrame()) {
// _currentFrame = currentAnimationProperties.getFirstFrame();
// }
// current frame greater than lastframe is dealt with in updateframe.
//_lastAnimated = usecTimestampNow();
qCDebug(entities) << "point 3 " << _currentFrame;
}
});
_previousAnimationProperties = this->getAnimationProperties();
//qCDebug(entities) << "point 4 " << _currentFrame;
}
else {
qCDebug(entities) << "this is where the _currentFrame change is handled in the ModelEntityItem.cpp code, current frame is: " << currentAnimationProperties.getCurrentFrame();
//if the first frame is less than zero that is an error, so don't do anything.
if (!(getAnimationFirstFrame() < 0)) {
//if the current frame is less than zero then we are restarting the server.
if (_currentFrame < 0) {
qCDebug(entities) << "point 3.5 " << _currentFrame;
//_previousAnimationProperties = currentAnimationProperties;
if ((currentAnimationProperties.getCurrentFrame() < currentAnimationProperties.getLastFrame()) && (currentAnimationProperties.getCurrentFrame() > currentAnimationProperties.getFirstFrame())) {
_currentFrame = currentAnimationProperties.getCurrentFrame();
}
else {
_currentFrame = currentAnimationProperties.getFirstFrame();
setAnimationCurrentFrame(_currentFrame);
_lastAnimated = 0;
}
}
}
}
//_previousAnimationProperties = currentAnimationProperties;
updateFrameCount();
if (isAnimatingSomething()) {
if (!(getAnimationFirstFrame() < 0) && !(getAnimationFirstFrame() > getAnimationLastFrame()) ) {
updateFrameCount();
}
}
}
}
@ -252,9 +286,6 @@ void ModelEntityItem::updateFrameCount() {
auto interval = now - _lastAnimated;
_lastAnimated = now;
//here we implement the looping animation property
//get entity anim props
@ -264,27 +295,36 @@ void ModelEntityItem::updateFrameCount() {
bool isHolding = getAnimationHold();
int updatedFrameCount = lastFrame - firstFrame + 1;
if (isLooping || (_currentFrame < _previousAnimationProperties.getLastFrame())) {
//else advance the current frame.
//if hold or not playing don't advance the current frame.
//also if the animFrame is outside of first or last frame then don't advance the motion.
if (!isHolding && getAnimationIsPlaying() && !(_previousAnimationProperties.getCurrentFrame() > _previousAnimationProperties.getLastFrame()) && !(_previousAnimationProperties.getCurrentFrame() < _previousAnimationProperties.getFirstFrame())) {
float deltaTime = (float)interval / (float)USECS_PER_SECOND;
_currentFrame += (deltaTime * _previousAnimationProperties.getFPS());
while ((_currentFrame - _previousAnimationProperties.getFirstFrame()) > updatedFrameCount) {
_currentFrame -= updatedFrameCount;
}
qCDebug(entities) << "the frame is now 1 " << _currentFrame;
// setAnimationCurrentlyPlayingFrame(_currentlyPlayingFrame);
setAnimationCurrentFrame(_currentFrame);
}
} else {
_currentFrame = getAnimationLastFrame();
//qCDebug(entities) << "point 5 " << _currentFrame;
if (!isHolding && getAnimationIsPlaying()) {
float deltaTime = (float)interval / (float)USECS_PER_SECOND;
_currentFrame += (deltaTime * getAnimationFPS());
if (_currentFrame > getAnimationLastFrame()) {
if (isLooping) {
while ((_currentFrame - getAnimationFirstFrame()) > (updatedFrameCount - 1)) {
_currentFrame -= (updatedFrameCount - 1);
}
}
else {
_currentFrame = getAnimationLastFrame();
}
}
else if (_currentFrame < getAnimationFirstFrame()) {
if (getAnimationFirstFrame() < 0) {
_currentFrame = 0;
}
else {
_currentFrame = getAnimationFirstFrame();
}
}
setAnimationCurrentFrame(_currentFrame);
qCDebug(entities) << "the frame is now 2 " << _currentFrame;
}
//qCDebug(entities) << "_currentFrame is " << _currentFrame;
}
//angus
@ -706,6 +746,13 @@ float ModelEntityItem::getAnimationCurrentFrame() const {
});
}
float ModelEntityItem::getAnimationFPS() const {
return resultWithReadLock<float>([&] {
return _animationProperties.getFPS();
});
}
//angus change
bool ModelEntityItem::isAnimatingSomething() const {
return resultWithReadLock<bool>([&] {

View file

@ -108,6 +108,7 @@ public:
bool getAnimationIsPlaying() const;
float getAnimationCurrentFrame() const;
float getAnimationFPS() const;
bool isAnimatingSomething() const;
quint64 getCurrentlyPlayingFrame() const;
@ -178,7 +179,7 @@ private:
AnimationPropertyGroup _previousAnimationProperties;
bool _propTestFlag{ true };
bool _propTestFlag2{ true };
float _currentFrame{ 0 };
float _currentFrame{ -1 };
//angus
};