From 47c64e3bf342094db0b5648717a15e2e13589626 Mon Sep 17 00:00:00 2001 From: Epicalert Date: Tue, 13 Apr 2021 15:52:46 +0800 Subject: [PATCH] Add smoothing Transform of each part is now smoothed over 4 frames to stop shaking. --- TODO.md | 5 ++++- src/model.cpp | 1 - src/modelpart.cpp | 27 ++++++++++++++++++++++++++- src/modelpart.hpp | 9 +++++++++ 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/TODO.md b/TODO.md index 7565add..41ccc35 100644 --- a/TODO.md +++ b/TODO.md @@ -1,7 +1,6 @@ # Before 1.0 ## graphics -- smoothing ## docs - getting started guide @@ -9,3 +8,7 @@ ## models - better default model + +## vision +- eye open/closed +- eye look direction diff --git a/src/model.cpp b/src/model.cpp index 11b0261..4b0e876 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -140,7 +140,6 @@ Model::Model(const char* path) { } // offsets - auto posOffsetArray = partsVec[i].getArray("pos_offset"); if (posOffsetArray) { auto offsetVec = *posOffsetArray->getDoubleVector(); diff --git a/src/modelpart.cpp b/src/modelpart.cpp index d4343fe..d5001b6 100644 --- a/src/modelpart.cpp +++ b/src/modelpart.cpp @@ -40,6 +40,31 @@ void ModelPart::setFollowTarget(std::string followTarget) { follow = bindStringToNum[followTarget]; } +void ModelPart::smoothTransform(glm::vec2 position, float rotation, float scale) { + histPositions[histI] = position; + histRotations[histI] = rotation; + histScales[histI] = scale; + + histI++; + histI = histI % SMOOTHING_FRAMES; + + glm::vec2 smoothedPosition(0,0); + float smoothedRotation = 0; + float smoothedScale = 0; + + for(size_t i = 0; i < SMOOTHING_FRAMES; i++) { + smoothedPosition += histPositions[i]; + smoothedRotation += histRotations[i]; + smoothedScale += histScales[i]; + } + + smoothedPosition /= SMOOTHING_FRAMES; + smoothedRotation /= SMOOTHING_FRAMES; + smoothedScale /= SMOOTHING_FRAMES; + + setTransform(smoothedPosition, smoothedRotation, smoothedScale); +} + void ModelPart::setTransform(glm::vec2 position, float rotation, float scale) { transMatrix = glm::ortho(-windowAspectRatio, windowAspectRatio, -1.0f, 1.0f); transMatrix = glm::translate(transMatrix, glm::vec3(position.x, -position.y, 0.0f) + glm::vec3(posOffset, 0.0f)); @@ -55,7 +80,7 @@ void ModelPart::processFaceData(struct FaceData faceData) { glm::vec2 followDirection = followPosition - bindPosition; glm::vec2 newPosition = bindPosition + (followDirection * factor); - setTransform(newPosition, faceData.headRotation, faceData.scale); + smoothTransform(newPosition, faceData.headRotation, faceData.scale); // change textures selectTexture(0); // if none are triggered, use the first one diff --git a/src/modelpart.hpp b/src/modelpart.hpp index 4d14edb..63db13e 100644 --- a/src/modelpart.hpp +++ b/src/modelpart.hpp @@ -14,6 +14,9 @@ #define TRIGGER_NULL 0x00 #define TRIGGER_MOUTH_OPEN 0x01 +// number of frames to average for smoothing +#define SMOOTHING_FRAMES 4 + class ModelPart { GLuint vao; @@ -25,6 +28,11 @@ class ModelPart { glm::mat4 transMatrix = glm::mat4(1.0f); bool empty = true; + + glm::vec2 histPositions[SMOOTHING_FRAMES]; + float histRotations[SMOOTHING_FRAMES]; + float histScales[SMOOTHING_FRAMES]; + size_t histI = 0; public: int bind = BIND_NULL; @@ -47,6 +55,7 @@ class ModelPart { void setBind(std::string bindName); void setFollowTarget(std::string followTarget); + void smoothTransform(glm::vec2 position, float rotation, float scale); void setTransform(glm::vec2 position, float rotation, float scale); void processFaceData(struct FaceData faceData);