blocks animation fix

This commit is contained in:
MihailRis 2024-01-23 23:03:52 +03:00
parent 4fe79458ab
commit 36a5da5bc1
2 changed files with 65 additions and 56 deletions

View File

@ -5,53 +5,62 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <unordered_set> #include <unordered_set>
TextureAnimator::TextureAnimator() : TextureAnimator::TextureAnimator() {
frameBuffer(new Framebuffer(1u, 1u)) glGenFramebuffers(1, &fboR);
{ glGenFramebuffers(1, &fboD);
} }
TextureAnimator::~TextureAnimator() { TextureAnimator::~TextureAnimator() {
delete frameBuffer; glDeleteFramebuffers(1, &fboR);
glDeleteFramebuffers(1, &fboD);
} }
void TextureAnimator::addAnimations(const std::vector<TextureAnimation>& animations) { void TextureAnimator::addAnimations(const std::vector<TextureAnimation>& animations) {
for (const auto& elem : animations) { for (const auto& elem : animations) {
addAnimation(elem); addAnimation(elem);
} }
} }
void TextureAnimator::update(float delta) { void TextureAnimator::update(float delta) {
std::unordered_set<uint> changedTextures; std::unordered_set<uint> changedTextures;
frameBuffer->bind(); for (auto& elem : animations) {
for (auto& elem : animations) { elem.timer += delta;
elem.timer += delta; size_t frameNum = elem.currentFrame;
size_t frameNum = elem.currentFrame; Frame frame = elem.frames[elem.currentFrame];
Frame frame = elem.frames[elem.currentFrame]; while (elem.timer >= frame.duration) {
while (elem.timer >= frame.duration) { elem.timer -= frame.duration;
elem.timer -= frame.duration; elem.currentFrame++;
elem.currentFrame++; if (elem.currentFrame >= elem.frames.size()) elem.currentFrame = 0;
if (elem.currentFrame >= elem.frames.size()) elem.currentFrame = 0; frame = elem.frames[elem.currentFrame];
frame = elem.frames[elem.currentFrame]; }
} if (frameNum != elem.currentFrame){
if (frameNum != elem.currentFrame){ if (changedTextures.find(elem.dstTexture->id) == changedTextures.end()) changedTextures.insert(elem.dstTexture->id);
if (changedTextures.find(elem.dstTexture->id) == changedTextures.end()) changedTextures.insert(elem.dstTexture->id);
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, elem.srcTexture->id, 0); glBindFramebuffer(GL_FRAMEBUFFER, fboD);
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, elem.dstTexture->id, 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, elem.dstTexture->id, 0);
glDrawBuffer(GL_COLOR_ATTACHMENT1);
float srcPosY = elem.srcTexture->height - frame.size.y - frame.srcPos.y; // vertical flip glBindFramebuffer(GL_FRAMEBUFFER, fboR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, elem.srcTexture->id, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBlitFramebuffer(frame.srcPos.x, srcPosY, frame.srcPos.x + frame.size.x, srcPosY + frame.size.y, glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboD);
frame.dstPos.x, frame.dstPos.y, frame.dstPos.x + frame.size.x, frame.dstPos.y + frame.size.y, glBindFramebuffer(GL_READ_FRAMEBUFFER, fboR);
GL_COLOR_BUFFER_BIT, GL_NEAREST);
} float srcPosY = elem.srcTexture->height - frame.size.y - frame.srcPos.y; // vertical flip
}
frameBuffer->unbind(); glBlitFramebuffer(frame.srcPos.x, srcPosY, frame.srcPos.x + frame.size.x, srcPosY + frame.size.y,
for (auto& elem : changedTextures) { frame.dstPos.x, frame.dstPos.y, frame.dstPos.x + frame.size.x, frame.dstPos.y + frame.size.y,
glBindTexture(GL_TEXTURE_2D, elem); GL_COLOR_BUFFER_BIT, GL_NEAREST);
glGenerateMipmap(GL_TEXTURE_2D); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
} glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);
}
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
for (auto& elem : changedTextures) {
glBindTexture(GL_TEXTURE_2D, elem);
glGenerateMipmap(GL_TEXTURE_2D);
}
glBindTexture(GL_TEXTURE_2D, 0);
} }

View File

@ -13,40 +13,40 @@ class Framebuffer;
constexpr float DEFAULT_FRAME_DURATION = 0.150f; constexpr float DEFAULT_FRAME_DURATION = 0.150f;
struct Frame { struct Frame {
glm::ivec2 srcPos; glm::ivec2 srcPos;
glm::ivec2 dstPos; glm::ivec2 dstPos;
glm::ivec2 size; glm::ivec2 size;
float duration = DEFAULT_FRAME_DURATION; float duration = DEFAULT_FRAME_DURATION;
}; };
class TextureAnimation { class TextureAnimation {
public: public:
TextureAnimation(Texture* srcTex, Texture* dstTex) : srcTexture(srcTex), dstTexture(dstTex) {}; TextureAnimation(Texture* srcTex, Texture* dstTex) : srcTexture(srcTex), dstTexture(dstTex) {};
~TextureAnimation() {}; ~TextureAnimation() {};
void addFrame(const Frame& frame) { frames.emplace_back(frame); }; void addFrame(const Frame& frame) { frames.emplace_back(frame); };
size_t currentFrame = 0; size_t currentFrame = 0;
float timer = 0.f; float timer = 0.f;
Texture* srcTexture; Texture* srcTexture;
Texture* dstTexture; Texture* dstTexture;
std::vector<Frame> frames; std::vector<Frame> frames;
}; };
class TextureAnimator { class TextureAnimator {
public: public:
TextureAnimator(); TextureAnimator();
~TextureAnimator(); ~TextureAnimator();
void addAnimation(const TextureAnimation& animation) { animations.emplace_back(animation); }; void addAnimation(const TextureAnimation& animation) { animations.emplace_back(animation); };
void addAnimations(const std::vector<TextureAnimation>& animations); void addAnimations(const std::vector<TextureAnimation>& animations);
void update(float delta); void update(float delta);
private: private:
uint fboR;
uint fboD;
Framebuffer* frameBuffer; std::vector<TextureAnimation> animations;
std::vector<TextureAnimation> animations;
}; };
#endif // !TEXTURE_ANIMATION_H #endif // !TEXTURE_ANIMATION_H