blocks animation fix
This commit is contained in:
parent
4fe79458ab
commit
36a5da5bc1
@ -5,53 +5,62 @@
|
||||
#include <GL/glew.h>
|
||||
#include <unordered_set>
|
||||
|
||||
TextureAnimator::TextureAnimator() :
|
||||
frameBuffer(new Framebuffer(1u, 1u))
|
||||
{
|
||||
TextureAnimator::TextureAnimator() {
|
||||
glGenFramebuffers(1, &fboR);
|
||||
glGenFramebuffers(1, &fboD);
|
||||
}
|
||||
|
||||
TextureAnimator::~TextureAnimator() {
|
||||
delete frameBuffer;
|
||||
glDeleteFramebuffers(1, &fboR);
|
||||
glDeleteFramebuffers(1, &fboD);
|
||||
}
|
||||
|
||||
void TextureAnimator::addAnimations(const std::vector<TextureAnimation>& animations) {
|
||||
for (const auto& elem : animations) {
|
||||
addAnimation(elem);
|
||||
}
|
||||
for (const auto& elem : animations) {
|
||||
addAnimation(elem);
|
||||
}
|
||||
}
|
||||
|
||||
void TextureAnimator::update(float delta) {
|
||||
std::unordered_set<uint> changedTextures;
|
||||
std::unordered_set<uint> changedTextures;
|
||||
|
||||
frameBuffer->bind();
|
||||
for (auto& elem : animations) {
|
||||
elem.timer += delta;
|
||||
size_t frameNum = elem.currentFrame;
|
||||
Frame frame = elem.frames[elem.currentFrame];
|
||||
while (elem.timer >= frame.duration) {
|
||||
elem.timer -= frame.duration;
|
||||
elem.currentFrame++;
|
||||
if (elem.currentFrame >= elem.frames.size()) elem.currentFrame = 0;
|
||||
frame = elem.frames[elem.currentFrame];
|
||||
}
|
||||
if (frameNum != elem.currentFrame){
|
||||
if (changedTextures.find(elem.dstTexture->id) == changedTextures.end()) changedTextures.insert(elem.dstTexture->id);
|
||||
for (auto& elem : animations) {
|
||||
elem.timer += delta;
|
||||
size_t frameNum = elem.currentFrame;
|
||||
Frame frame = elem.frames[elem.currentFrame];
|
||||
while (elem.timer >= frame.duration) {
|
||||
elem.timer -= frame.duration;
|
||||
elem.currentFrame++;
|
||||
if (elem.currentFrame >= elem.frames.size()) elem.currentFrame = 0;
|
||||
frame = elem.frames[elem.currentFrame];
|
||||
}
|
||||
if (frameNum != elem.currentFrame){
|
||||
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);
|
||||
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, elem.dstTexture->id, 0);
|
||||
glDrawBuffer(GL_COLOR_ATTACHMENT1);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, fboD);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, elem.dstTexture->id, 0);
|
||||
|
||||
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,
|
||||
frame.dstPos.x, frame.dstPos.y, frame.dstPos.x + frame.size.x, frame.dstPos.y + frame.size.y,
|
||||
GL_COLOR_BUFFER_BIT, GL_NEAREST);
|
||||
}
|
||||
}
|
||||
frameBuffer->unbind();
|
||||
for (auto& elem : changedTextures) {
|
||||
glBindTexture(GL_TEXTURE_2D, elem);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
}
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboD);
|
||||
glBindFramebuffer(GL_READ_FRAMEBUFFER, fboR);
|
||||
|
||||
float srcPosY = elem.srcTexture->height - frame.size.y - frame.srcPos.y; // vertical flip
|
||||
|
||||
glBlitFramebuffer(frame.srcPos.x, srcPosY, frame.srcPos.x + frame.size.x, srcPosY + frame.size.y,
|
||||
frame.dstPos.x, frame.dstPos.y, frame.dstPos.x + frame.size.x, frame.dstPos.y + frame.size.y,
|
||||
GL_COLOR_BUFFER_BIT, GL_NEAREST);
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
||||
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
|
||||
|
||||
}
|
||||
}
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
for (auto& elem : changedTextures) {
|
||||
glBindTexture(GL_TEXTURE_2D, elem);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
}
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
@ -13,40 +13,40 @@ class Framebuffer;
|
||||
constexpr float DEFAULT_FRAME_DURATION = 0.150f;
|
||||
|
||||
struct Frame {
|
||||
glm::ivec2 srcPos;
|
||||
glm::ivec2 dstPos;
|
||||
glm::ivec2 size;
|
||||
float duration = DEFAULT_FRAME_DURATION;
|
||||
glm::ivec2 srcPos;
|
||||
glm::ivec2 dstPos;
|
||||
glm::ivec2 size;
|
||||
float duration = DEFAULT_FRAME_DURATION;
|
||||
};
|
||||
|
||||
class TextureAnimation {
|
||||
public:
|
||||
TextureAnimation(Texture* srcTex, Texture* dstTex) : srcTexture(srcTex), dstTexture(dstTex) {};
|
||||
~TextureAnimation() {};
|
||||
TextureAnimation(Texture* srcTex, Texture* dstTex) : srcTexture(srcTex), dstTexture(dstTex) {};
|
||||
~TextureAnimation() {};
|
||||
|
||||
void addFrame(const Frame& frame) { frames.emplace_back(frame); };
|
||||
void addFrame(const Frame& frame) { frames.emplace_back(frame); };
|
||||
|
||||
size_t currentFrame = 0;
|
||||
float timer = 0.f;
|
||||
Texture* srcTexture;
|
||||
Texture* dstTexture;
|
||||
std::vector<Frame> frames;
|
||||
size_t currentFrame = 0;
|
||||
float timer = 0.f;
|
||||
Texture* srcTexture;
|
||||
Texture* dstTexture;
|
||||
std::vector<Frame> frames;
|
||||
};
|
||||
|
||||
class TextureAnimator {
|
||||
public:
|
||||
TextureAnimator();
|
||||
~TextureAnimator();
|
||||
TextureAnimator();
|
||||
~TextureAnimator();
|
||||
|
||||
void addAnimation(const TextureAnimation& animation) { animations.emplace_back(animation); };
|
||||
void addAnimations(const std::vector<TextureAnimation>& animations);
|
||||
void addAnimation(const TextureAnimation& animation) { animations.emplace_back(animation); };
|
||||
void addAnimations(const std::vector<TextureAnimation>& animations);
|
||||
|
||||
void update(float delta);
|
||||
void update(float delta);
|
||||
private:
|
||||
uint fboR;
|
||||
uint fboD;
|
||||
|
||||
Framebuffer* frameBuffer;
|
||||
|
||||
std::vector<TextureAnimation> animations;
|
||||
std::vector<TextureAnimation> animations;
|
||||
};
|
||||
|
||||
#endif // !TEXTURE_ANIMATION_H
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user