From b16fb85192982a63b97a110eeaf0b721d5cd110a Mon Sep 17 00:00:00 2001 From: LockedLunatic Date: Mon, 20 Jun 2016 01:55:59 +0200 Subject: [PATCH] Lever - Door started Animations get reverted properly when reversing time green objects --- Weave/Game.cpp | 21 ++++++-- Weave/Graphix/Model/IAnimMesh.cpp | 45 ++--------------- Weave/Graphix/Model/IAnimMesh.h | 11 +--- Weave/Graphix/Model/IMetaMesh.cpp | 16 ++---- Weave/Graphix/Model/IMetaMesh.h | 4 +- Weave/Graphix/Model/Model.cpp | 14 +++--- Weave/Graphix/Model/Model.h | 5 +- Weave/Scene/EventBox.cpp | 4 +- Weave/Scene/Lever.cpp | 40 +++++++++++++++ Weave/Scene/Lever.h | 21 ++++++++ Weave/Scene/Scene.cpp | 63 +++++++++++++++++++---- Weave/Scene/Scene.h | 12 +++++ Weave/Scene/SceneObject.cpp | 84 ++++++++++++++++++++++++++++--- Weave/Scene/SceneObject.h | 11 ++++ Weave/Weave.vcxproj | 2 + Weave/Weave.vcxproj.filters | 6 +++ 16 files changed, 264 insertions(+), 95 deletions(-) create mode 100644 Weave/Scene/Lever.cpp create mode 100644 Weave/Scene/Lever.h diff --git a/Weave/Game.cpp b/Weave/Game.cpp index 3b9dcf9..53b96d9 100644 --- a/Weave/Game.cpp +++ b/Weave/Game.cpp @@ -12,6 +12,7 @@ #include #include "Scene.h" +#include "Scene\Lever.h" #include "Graphix\Shader.h" #include "Graphix\Graphix.h" @@ -66,15 +67,29 @@ Game::Game() : playing(true) current_world->addObject(new SceneObject(translate(vec3(-5.f, .4f, 0.f)), vec4(3.0f, 3.f, 0.4f, 1.5f), "duck", "model_duck_2D.png")); SceneObject* door = new SceneObject(translate(vec3(2.f, .4f, 0.f)), vec4(3.0f, 3.f, 0.4f, 1.5f), "door", "model_duck_2D.png"); - SceneObject* lever = new SceneObject(translate(vec3(0.f, .4f, 0.f)), vec4(3.0f, 3.f, 0.4f, 1.5f), "lever", "model_duck_2D.png"); - lever->timeresistant = true; current_world->addObject(door); + door->startanimation((uint)0, 1.0f); + door->setAnimationLoop(true); + + SceneObject* door2 = new SceneObject(translate(vec3(1.f, .4f, 0.f)), vec4(3.0f, 1.f, 0.4f, 1.5f), "door", "model_duck_2D.png"); + current_world->addObject(door2); + door2->startanimation((uint)0, 1.0f); + door2->setAnimationLoop(true); + door2->timeresistant = true; + + Lever* lever = new Lever(translate(vec3(0.f, .4f, 0.f)), vec4(3.0f, 3.f, 0.4f, 1.5f), "lever", "model_duck_2D.png"); current_world->addObject(lever); + lever->startanimation((uint)0, 0.0f); + lever->setAnimationLoop(false); + lever->timeresistant = true; + lever->setup(door); + + //TODO register door to lever current_world->addObject(new SceneObject(translate(vec3(-5.f, 3.f, 0.f)), vec4(3.0f, 3.f, 0.4f, 1.5f), "SkyBox", "model_SkyBox_2D.png")); - current_world->addObject(new EventBox(translate(vec3(3.f, .4f, 0.f)),EB_LOSTZONE)); + current_world->addObject(new EventBox(translate(vec3(3.f, .4f, 0.f)), EB_LOSTZONE)); //current_world->addObject(new EventBox(translate(vec3(0.f, -5.f, 0.f))*glm::scale(vec3(50.f,1.f,50.f)), EB_LOSTZONE)); current_world->addObject(new EventBox(translate(vec3(3.f, .4f, -15.f)), EB_WINZONE)); diff --git a/Weave/Graphix/Model/IAnimMesh.cpp b/Weave/Graphix/Model/IAnimMesh.cpp index af46920..55da207 100644 --- a/Weave/Graphix/Model/IAnimMesh.cpp +++ b/Weave/Graphix/Model/IAnimMesh.cpp @@ -2,9 +2,7 @@ IAnimMesh::IAnimMesh(const aiMesh* mesh, const vec3& scale, bool isConvex) : IMesh(mesh, scale) { - currentAnimation = 0; - time = 0; - animationSpeed = 1.0f; + } IAnimMesh::~IAnimMesh() @@ -17,50 +15,17 @@ void IAnimMesh::addanimation(aiNodeAnim* animation) animations.push_back(new Animation(animation)); } -void IAnimMesh::startanimation(uint index, float speed) -{ - currentAnimation = index; - animationSpeed = 1.0f; - time = 0; -} - -void IAnimMesh::stopanimation() -{ - animationSpeed = 0; -} - -void IAnimMesh::drawModel(const mat4& _modelMat, drawType _type, const vec4& _color) const +void IAnimMesh::drawModel(const mat4& _modelMat, drawType _type, const vec4& _color, uint index, double time) const { if (animations.empty()) { - Model::drawModel(_modelMat, _type, _color); + Model::drawModel(_modelMat, _type, _color, index, time); } else { - Animation* anim = animations[currentAnimation]; + Animation* anim = animations[index]; mat4 mat = anim->getmodelmat(time); - Model::drawModel(_modelMat * mat, _type, _color); - } -} - -void IAnimMesh::updateModel(float deltaT) -{ - if (animations.empty()) - { - Model::updateModel(deltaT); - } - else - { - time += animationSpeed * deltaT; - while (time < 0) - { - time = time + animationduration; - } - while (time >= animationduration) - { - time = time - animationduration; - } - Model::updateModel(deltaT); + Model::drawModel(_modelMat * mat, _type, _color, index, time); } } \ No newline at end of file diff --git a/Weave/Graphix/Model/IAnimMesh.h b/Weave/Graphix/Model/IAnimMesh.h index 01c4ea9..a8da184 100644 --- a/Weave/Graphix/Model/IAnimMesh.h +++ b/Weave/Graphix/Model/IAnimMesh.h @@ -12,18 +12,9 @@ public: virtual ~IAnimMesh(); void addanimation(aiNodeAnim* animation); - void startanimation(uint index, float speed); - void stopanimation(); - virtual void drawModel(const mat4& modelMat, drawType type, const vec4& color) const; - - void updateModel(float deltaT); - - double animationduration; + virtual void drawModel(const mat4& modelMat, drawType type, const vec4& color, uint index, double time) const; protected: std::vector animations; - double time; - uint currentAnimation; - float animationSpeed; }; \ No newline at end of file diff --git a/Weave/Graphix/Model/IMetaMesh.cpp b/Weave/Graphix/Model/IMetaMesh.cpp index 8a7ea46..f440c89 100644 --- a/Weave/Graphix/Model/IMetaMesh.cpp +++ b/Weave/Graphix/Model/IMetaMesh.cpp @@ -57,6 +57,8 @@ IMetaMesh::IMetaMesh(const string& _modelpath, const vec3& _scale, bool _isConve { IAnimMesh* tmpIMesh; + animationduration = scene->mAnimations[0]->mDuration; + for (uint i = 0; i < scene->mNumMeshes; i++) { bool animated = false; @@ -83,8 +85,6 @@ IMetaMesh::IMetaMesh(const string& _modelpath, const vec3& _scale, bool _isConve tmpModelMatClean = removeScale(tmpModelMat); tmpIMesh = new IAnimMesh(scene->mMeshes[i], _scale* getScale(tmpModelMat)); - tmpIMesh->animationduration = scene->mAnimations[0]->mDuration; - for (uint l = 0; l < scene->mNumAnimations; l++) { aiAnimation* tmpanimation = scene->mAnimations[l]; @@ -202,11 +202,11 @@ void IMetaMesh::unbindShader(const Shader* _shader) } } -void IMetaMesh::drawModel(const mat4& _modelMat, drawType _type, const vec4& _color) const +void IMetaMesh::drawModel(const mat4& _modelMat, drawType _type, const vec4& _color, uint index, double time) const { for (auto i = models.begin(); i != models.end(); ++i) { - i->first->drawModel(_modelMat* i->second, _type, _color*.9f); + i->first->drawModel(_modelMat* i->second, _type, _color*.9f, index, time); } } @@ -218,14 +218,6 @@ void IMetaMesh::drawModel(const mat4& _modelMat, drawType _type, const vec4& _co // } //} -void IMetaMesh::updateModel(float deltaT) -{ - for (auto i = models.begin(); i != models.end(); ++i) - { - i->first->updateModel(deltaT); - } -} - void IMetaMesh::bt_init(bool _isConvex) { if (bt_collision_shape == nullptr) diff --git a/Weave/Graphix/Model/IMetaMesh.h b/Weave/Graphix/Model/IMetaMesh.h index 9358bf9..a1c43fd 100644 --- a/Weave/Graphix/Model/IMetaMesh.h +++ b/Weave/Graphix/Model/IMetaMesh.h @@ -22,13 +22,11 @@ public: void unbindModel() override; void unbindShader(const Shader* shader) override; - virtual void drawModel(const mat4& modelMat, drawType type = dr_Model, const vec4& color = vec4(0.9f, 0.f, 0.f, 1.f)) const override; + virtual void drawModel(const mat4& modelMat, drawType type = dr_Model, const vec4& color = vec4(0.9f, 0.f, 0.f, 1.f), uint index = 0, double animationtime = 0) const override; //void drawWire(const mat4& modelMat, const vec4& _color = vec4(0.9f, 0.f, 0.f, 1.f)) const override; //void drawBBox(const mat4& modelMat, const vec4& color = vec4(0.9f, 0.f, 0.f, 1.f)) const override; - void updateModel(float deltaT) override; - virtual void bt_init(bool isConvex = true) override; std::string type2str() const override; diff --git a/Weave/Graphix/Model/Model.cpp b/Weave/Graphix/Model/Model.cpp index 45508fc..2c08731 100644 --- a/Weave/Graphix/Model/Model.cpp +++ b/Weave/Graphix/Model/Model.cpp @@ -25,6 +25,7 @@ typedef Model::Box Box; Model::Model() { + animationduration = 0; } Model::~Model() @@ -131,7 +132,7 @@ void Model::drawWire() const glBindVertexArray(0); } -void Model::drawModel(const mat4& _modelMat, drawType _type, const vec4& _color) const +void Model::drawModel(const mat4& _modelMat, drawType _type, const vec4& _color, uint animation, double time) const { useModel(_modelMat); @@ -165,8 +166,12 @@ void Model::drawModel(const mat4& _modelMat, drawType _type, const vec4& _color) getBBoxModel()->drawModel(_modelMat*translate(center)*glm::scale(size)); break; } +} - +void Model::drawModel(const mat4& _modelMat) const +{ + useModel(_modelMat); + drawModel(); } //void Model::drawModel(const mat4& _modelMat) const @@ -437,11 +442,6 @@ bool Model::loadMesh(const aiMesh* mesh, const vec3& _scale) return true; } -void Model::updateModel(float deltaT) -{ - -} - void Model::box_init(const mat4& _model) { if (vertex == nullptr) diff --git a/Weave/Graphix/Model/Model.h b/Weave/Graphix/Model/Model.h index 2f7693d..2f162a9 100644 --- a/Weave/Graphix/Model/Model.h +++ b/Weave/Graphix/Model/Model.h @@ -33,7 +33,8 @@ public: virtual void unbindShader(const Shader* shader); /* Draws Model */ - virtual void drawModel(const mat4& modelMat, drawType type = dr_Model, const vec4& color = vec4(0.9f, 0.f, 0.f, 1.f)) const; + virtual void drawModel(const mat4& modelMat, drawType type, const vec4& color, uint animation, double time) const; + void drawModel(const mat4& modelMat) const; //virtual void drawModel(const mat4& modelMat) const; //virtual void drawWire(const mat4& modelMat, const vec4& color) const; //virtual void drawWire(const mat4& modelMat) const; @@ -64,7 +65,7 @@ public: bool loadMesh(const std::string& modelpath, uint mindex = 0); bool loadMesh(const aiMesh* mesh, const vec3& scale = vec3(1.f)); - virtual void updateModel(float deltaT); + double animationduration; class Box { diff --git a/Weave/Scene/EventBox.cpp b/Weave/Scene/EventBox.cpp index e924fa6..4047f50 100644 --- a/Weave/Scene/EventBox.cpp +++ b/Weave/Scene/EventBox.cpp @@ -52,8 +52,8 @@ void EventBox::update(float _deltaT) void EventBox::draw(drawTarget _target) const { -if (_target == DRAW_Coll) - collision->drawModel(modelMat, dr_BBox, vec4(.3f,.9f,.9f,1.f)); + if (_target == DRAW_Coll) + collision->drawModel(modelMat, dr_BBox, vec4(.3f,.9f,.9f,1.f), 0, 0); } void EventBox::collides(SceneObject* _other, btPersistentManifold* _contactManifold, float _deltaT) diff --git a/Weave/Scene/Lever.cpp b/Weave/Scene/Lever.cpp new file mode 100644 index 0000000..f03c17f --- /dev/null +++ b/Weave/Scene/Lever.cpp @@ -0,0 +1,40 @@ +#include "Lever.h" + +Lever::Lever(const mat4& _modelMat, const vec4& _material, string _modelpath, string texturepath) : SceneObject(_modelMat, _material, _modelpath, texturepath) +{ + recipient = nullptr; +} + +Lever::Lever(const mat4& modelMat, const vec4& material, Model* model, string texturepath) : SceneObject(modelMat, material, model, texturepath) +{ + recipient = nullptr; +} + +Lever::~Lever() +{ + +} + +void Lever::setup(SceneObject* object) +{ + recipient = object; +} + +void Lever::trigger(bool state) +{ + if (turned != state) + { + turned = state; + //TODO add event + if (state) + { + this->startanimation((uint) 0, 1.0f); + recipient->startanimation((uint) 0, 1.0f); + } + else + { + this->startanimation((uint)0, -1.0f); + recipient->startanimation((uint)0, -1.0f); + } + } +} \ No newline at end of file diff --git a/Weave/Scene/Lever.h b/Weave/Scene/Lever.h new file mode 100644 index 0000000..c24f5d5 --- /dev/null +++ b/Weave/Scene/Lever.h @@ -0,0 +1,21 @@ +#pragma once + +#include "../GLM.h" + +#include "SceneObject.h" + +typedef unsigned int uint; + +class Lever : public SceneObject +{ +public: + Lever(const mat4& _modelMat, const vec4& _material, string _modelpath, string texturepath); + Lever(const mat4& modelMat, const vec4& material, Model* model, string texturepath); + ~Lever(); + void setup(SceneObject* object); + void trigger(bool state); + +protected: + SceneObject* recipient; + bool turned; +}; \ No newline at end of file diff --git a/Weave/Scene/Scene.cpp b/Weave/Scene/Scene.cpp index f6b2b6a..a02a3c3 100644 --- a/Weave/Scene/Scene.cpp +++ b/Weave/Scene/Scene.cpp @@ -55,6 +55,12 @@ lookat(_lookat) if (lookat != nullptr) timestamps.push(Timestamp(0.0f, _lookat->getModelMat(), lookat->ySpeed)); + TimeEvent dummy; + dummy.time = 0.0f; + dummy.object = nullptr; + dummy.id = -1; + events.push(dummy); + bt_collision_configuration = new btDefaultCollisionConfiguration(); bt_dispatcher = new btCollisionDispatcher(bt_collision_configuration); @@ -202,11 +208,15 @@ void Scene::update(float deltaT) } } + + currenttime = max(0.0f, currenttime - deltaT); + + float prev_time = timestamps.top().time; mat4 prev_mat = timestamps.top().Playermodel; //unsigned int prev_camera = timestamps.top().CameraX; - int prev_ySpeed = timestamps.top().ySpeed; - while (max(0.0f, currenttime - deltaT) < prev_time) + float prev_ySpeed = timestamps.top().ySpeed; + while (currenttime < prev_time) { timestamps.pop(); @@ -217,20 +227,21 @@ void Scene::update(float deltaT) } - //TODO revert events + while (currenttime < events.top().time) + { + TimeEvent tmp = events.top(); + events.pop(); + + revertEvent(tmp); + } + + //TODO go from keyframe to current time lookat->setModel(prev_mat); lookat->ySpeed = prev_ySpeed; lookat->movable = true; - //for (auto i = SceneObjects.begin(); i != SceneObjects.end(); ++i) - //{ - // //(*i)->update(-max(0.0f, currenttime - deltaT)); - //} - - - currenttime = max(0.0f, currenttime - deltaT); } camera.updateView(lookat->getPosition()+vec3(0.f,1.f,0.f)); @@ -509,4 +520,36 @@ void Scene::bindShader() +} + +void Scene::addEvent(SceneObject* _object, int _id) +{ + TimeEvent newevent; + newevent.time = currenttime; + newevent.object = _object; + newevent.id = _id; + events.push(newevent); +} + +void Scene::revertEvent(TimeEvent e) +{ + switch (e.id) + { + case 0: + //forward Animation ended + e.object->startanimation((uint) 0, -1.0f); + e.object->setanimationtime(e.time - currenttime); + break; + + case 1: + //reverse Animation ended + e.object->startanimation((uint) 0, 1.0f); + e.object->setanimationtime(e.time - currenttime); + break; + + case 2: + //Lever triggered + + break; + } } \ No newline at end of file diff --git a/Weave/Scene/Scene.h b/Weave/Scene/Scene.h index 96abd01..6f8b24f 100644 --- a/Weave/Scene/Scene.h +++ b/Weave/Scene/Scene.h @@ -20,6 +20,13 @@ class Shader; class fBufferObject; //class Camera; +struct TimeEvent +{ + float time; + SceneObject* object; + int id; +}; + class Scene { public: @@ -44,6 +51,8 @@ public: void setView(unsigned int width, unsigned int height, bool updateProjection = true); + void addEvent(SceneObject* _object, int _id); + protected: //ViewPort viewPort; Camera camera; @@ -54,10 +63,13 @@ protected: std::set ShaderSet; void drawSceneObjects(drawTarget target = DRAW_Model, bool culling = true) const; + + void revertEvent(TimeEvent e); float currenttime; std::stack timestamps; + std::stack events; //std::stack PlayerModels; //std::stack PlayerDirections; diff --git a/Weave/Scene/SceneObject.cpp b/Weave/Scene/SceneObject.cpp index 505e015..5957192 100644 --- a/Weave/Scene/SceneObject.cpp +++ b/Weave/Scene/SceneObject.cpp @@ -44,7 +44,11 @@ newModel(true), collide_group(0), collide_with(0), bt_collision_object(new btCollisionObject()), -move_delta(0) +move_delta(0), +AnimationTime(0), +currentAnimation(0), +animationSpeed(0), +loopanimation(true) { //Message::info("Error from befor?"); //Graphix::getGlError(); @@ -142,7 +146,11 @@ collide_group(0), collide_with(0), bt_collision_object(new btCollisionObject()), bt_rigid_body(nullptr), -move_delta(0) +move_delta(0), +AnimationTime(0), +currentAnimation(0), +animationSpeed(0), +loopanimation(true) { //Message::info("Error from befor?"); //Graphix::getGlError(); @@ -226,7 +234,7 @@ void SceneObject::update(float deltaT) } - model->updateModel(deltaT); + updateAnimation(deltaT); btTransform tmp; tmp.setFromOpenGLMatrix(value_ptr(modelMat)); @@ -246,15 +254,15 @@ void SceneObject::draw(drawTarget _target) const case DRAW_Model: default: texture->useTexture(); - model->drawModel(modelMat,dr_Model); + model->drawModel(modelMat, dr_Model, vec4(0.9f, 0.f, 0.f, 1.f), currentAnimation, AnimationTime); break; case DRAW_Wire: texture->useTexture(); - model->drawModel(modelMat, dr_WireT); + model->drawModel(modelMat, dr_WireT, vec4(0.9f, 0.f, 0.f, 1.f), currentAnimation, AnimationTime); break; case DRAW_Coll: if (collision != nullptr) - collision->drawModel(modelMat, dr_WireC, vec4(0.9f, 0.f, 0.f, 1.f)); + collision->drawModel(modelMat, dr_WireC, vec4(0.9f, 0.f, 0.f, 1.f), currentAnimation, AnimationTime); break; } /* Draw Object*/ @@ -389,3 +397,67 @@ short SceneObject::getCollideWith() const return collide_with; } +void SceneObject::startanimation(uint index, float speed) +{ + currentAnimation = index; + animationSpeed = speed; + if (speed >= 0) + { + AnimationTime = 0; + } + else + { + AnimationTime = model->animationduration - 0.01f; + } +} + +void SceneObject::setanimationtime(float _time) +{ + if (animationSpeed >= 0) + { + AnimationTime = _time; + } + else + { + AnimationTime = model->animationduration - _time; + } +} + +void SceneObject::setAnimationLoop(bool loop) +{ + loopanimation = loop; +} + +void SceneObject::updateAnimation(float deltaT) +{ + if (animationSpeed != 0) + { + AnimationTime += animationSpeed * deltaT; + while (AnimationTime < 0) + { + if (loopanimation) + { + AnimationTime = AnimationTime + model->animationduration; + } + else + { + AnimationTime = 0; + animationSpeed = 0; + //TODO add event + } + } + while (AnimationTime >= model->animationduration) + { + if (loopanimation) + { + AnimationTime = AnimationTime - model->animationduration; + } + else + { + AnimationTime = model->animationduration - 0.01f; + animationSpeed = 0; + //TODO add event + } + } + } +} \ No newline at end of file diff --git a/Weave/Scene/SceneObject.h b/Weave/Scene/SceneObject.h index 2abcdea..ffbb79f 100644 --- a/Weave/Scene/SceneObject.h +++ b/Weave/Scene/SceneObject.h @@ -5,6 +5,8 @@ #include +typedef unsigned int uint; + using std::string; class Scene; @@ -87,6 +89,9 @@ public: float move_delta; float yFloorDist; + virtual void startanimation(uint index, float speed); + virtual void setanimationtime(float time); + virtual void setAnimationLoop(bool loop); bool timeresistant; bool ignore; @@ -94,6 +99,8 @@ public: protected: + void updateAnimation(float); + mat4 modelMat; @@ -113,5 +120,9 @@ protected: btCollisionObject* bt_collision_object; btRigidBody* bt_rigid_body; + double AnimationTime; + uint currentAnimation; + float animationSpeed; + bool loopanimation; }; diff --git a/Weave/Weave.vcxproj b/Weave/Weave.vcxproj index 7cba953..1c03e71 100644 --- a/Weave/Weave.vcxproj +++ b/Weave/Weave.vcxproj @@ -116,6 +116,7 @@ + @@ -158,6 +159,7 @@ + diff --git a/Weave/Weave.vcxproj.filters b/Weave/Weave.vcxproj.filters index e649099..cc39f75 100644 --- a/Weave/Weave.vcxproj.filters +++ b/Weave/Weave.vcxproj.filters @@ -114,6 +114,9 @@ Source Files + + Source Files + @@ -233,5 +236,8 @@ Header Files + + Header Files + \ No newline at end of file -- 2.47.3