102 lines
2.9 KiB
C++
102 lines
2.9 KiB
C++
// Copyright (c) 2023 Dominic Masters
|
|
//
|
|
// This software is released under the MIT License.
|
|
// https://opensource.org/licenses/MIT
|
|
|
|
#include "TextureRenderTarget.hpp"
|
|
#include "assert/assertgl.hpp"
|
|
|
|
using namespace Dawn;
|
|
|
|
TextureRenderTarget::TextureRenderTarget(float_t width, float_t height) {
|
|
this->setSize(width, height);
|
|
}
|
|
|
|
Texture * TextureRenderTarget::getTexture() {
|
|
return &this->texture;
|
|
}
|
|
|
|
void TextureRenderTarget::setSize(float_t width, float_t height) {
|
|
assertTrue(width > 0, "TextureRenderTarget::setSize: Width must be greater than 0!");
|
|
assertTrue(height > 0, "TextureRenderTarget::setSize: Height must be greater than 0!");
|
|
|
|
if(width == this->getWidth() && height == this->getHeight()) return;
|
|
|
|
// Delete old buffers.
|
|
if(this->rboId != -1) glDeleteRenderbuffers(1, &this->rboId);
|
|
assertNoGLError();
|
|
if(this->fboId != -1) glDeleteFramebuffers(1, &this->fboId);
|
|
assertNoGLError();
|
|
|
|
// Resize texture
|
|
this->texture.setSize((int32_t)width, (int32_t)height, TEXTURE_FORMAT_RGBA, TEXTURE_DATA_FORMAT_FLOAT);
|
|
this->eventRenderTargetResized.invoke(this, width, height);
|
|
|
|
// Create Frame Buffer
|
|
glGenFramebuffers(1, &this->fboId);
|
|
assertNoGLError();
|
|
glBindFramebuffer(GL_FRAMEBUFFER, this->fboId);
|
|
assertNoGLError();
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
|
|
GL_TEXTURE_2D, this->texture.id, 0
|
|
);
|
|
assertNoGLError();
|
|
|
|
// Create Render Buffer
|
|
glGenRenderbuffers(1, &this->rboId);
|
|
assertNoGLError();
|
|
glBindRenderbuffer(GL_RENDERBUFFER, this->rboId);
|
|
assertNoGLError();
|
|
glRenderbufferStorage(
|
|
GL_RENDERBUFFER, GL_DEPTH24_STENCIL8,
|
|
this->texture.width, this->texture.height
|
|
);
|
|
assertNoGLError();
|
|
glBindRenderbuffer(GL_RENDERBUFFER, 0);
|
|
assertNoGLError();
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
|
|
GL_RENDERBUFFER, this->rboId
|
|
);
|
|
assertNoGLError();
|
|
|
|
// Validate things went correct.
|
|
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
|
|
assertUnreachable("TextureRenderTarget::setSize: Framebuffer is not complete!");
|
|
}
|
|
}
|
|
|
|
float_t TextureRenderTarget::getScale() {
|
|
return 1.0f;
|
|
}
|
|
|
|
float_t TextureRenderTarget::getWidth() {
|
|
return (float_t)this->texture.getWidth();
|
|
}
|
|
|
|
float_t TextureRenderTarget::getHeight() {
|
|
return (float_t)this->texture.getHeight();
|
|
}
|
|
|
|
void TextureRenderTarget::setClearColor(struct Color color) {
|
|
this->clearColor = color;
|
|
}
|
|
|
|
void TextureRenderTarget::clear(flag8_t clearFlags) {
|
|
glClearColor(
|
|
clearColor.r,
|
|
clearColor.g,
|
|
clearColor.b,
|
|
clearColor.a
|
|
);
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
}
|
|
|
|
void TextureRenderTarget::bind() {
|
|
glBindFramebuffer(GL_FRAMEBUFFER, this->fboId);
|
|
glViewport(0, 0, this->texture.getWidth(), this->texture.getHeight());
|
|
}
|
|
|
|
TextureRenderTarget::~TextureRenderTarget() {
|
|
if(this->rboId != -1) glDeleteRenderbuffers(1, &this->rboId);
|
|
if(this->fboId != -1) glDeleteFramebuffers(1, &this->fboId);
|
|
} |