49 lines
1.7 KiB
C++
49 lines
1.7 KiB
C++
#include "FrameBuffer.h"
|
|
#include <iostream>
|
|
#include "Environment.h"
|
|
|
|
namespace ZL {
|
|
|
|
FrameBuffer::FrameBuffer(int w, int h) : width(w), height(h) {
|
|
// 1. Ñîçäàåì FBO
|
|
glGenFramebuffers(1, &fbo);
|
|
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
|
|
|
// 2. Ñîçäàåì òåêñòóðó, êóäà áóäåì "ôîòîãðàôèðîâàòü"
|
|
glGenTextures(1, &textureID);
|
|
glBindTexture(GL_TEXTURE_2D, textureID);
|
|
|
|
// Óñòàíàâëèâàåì ïàðàìåòðû òåêñòóðû
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
// 3. Ïðèâÿçûâàåì òåêñòóðó ê FBO
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureID, 0);
|
|
|
|
// Ïðîâåðêà ãîòîâíîñòè
|
|
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
|
|
std::cerr << "Error: Framebuffer is not complete!" << std::endl;
|
|
}
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
}
|
|
|
|
FrameBuffer::~FrameBuffer() {
|
|
glDeleteFramebuffers(1, &fbo);
|
|
glDeleteTextures(1, &textureID);
|
|
}
|
|
|
|
void FrameBuffer::Bind() {
|
|
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
|
glViewport(0, 0, width, height); // Âàæíî: óñòàíàâëèâàåì âüþïîðò ïîä ðàçìåð òåêñòóðû
|
|
}
|
|
|
|
void FrameBuffer::Unbind() {
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
// Çäåñü æåëàòåëüíî âîçâðàùàòü âüþïîðò ê ðàçìåðàì ýêðàíà,
|
|
// íàïðèìåð, ÷åðåç Environment::width/height
|
|
glViewport(0, 0, Environment::width, Environment::height);
|
|
}
|
|
|
|
} // namespace ZL
|