Add smoothing

Transform of each part is now smoothed over 4 frames to stop shaking.
This commit is contained in:
Epicalert 2021-04-13 15:52:46 +08:00
parent a58e7d9428
commit 47c64e3bf3
No known key found for this signature in database
GPG key ID: CAA46F858D0979BD
4 changed files with 39 additions and 3 deletions

View file

@ -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

View file

@ -140,7 +140,6 @@ Model::Model(const char* path) {
}
// offsets
auto posOffsetArray = partsVec[i].getArray("pos_offset");
if (posOffsetArray) {
auto offsetVec = *posOffsetArray->getDoubleVector();

View file

@ -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

View file

@ -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;
@ -26,6 +29,11 @@ class ModelPart {
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;
int follow = 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);