summaryrefslogtreecommitdiffstats
path: root/src/Framebuffer.cpp
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2021-12-25 07:42:52 +0100
committerGitHub <noreply@github.com>2021-12-25 07:42:52 +0100
commit40421159568518bb7573903d14a2c69c64dead95 (patch)
tree15c830dce151bf0b427123efd214173742887e8e /src/Framebuffer.cpp
parentMerge pull request #70 from LaG1924/ftr/build-3 (diff)
parentFixed missing APIENTRY linux build (diff)
downloadAltCraft-40421159568518bb7573903d14a2c69c64dead95.tar
AltCraft-40421159568518bb7573903d14a2c69c64dead95.tar.gz
AltCraft-40421159568518bb7573903d14a2c69c64dead95.tar.bz2
AltCraft-40421159568518bb7573903d14a2c69c64dead95.tar.lz
AltCraft-40421159568518bb7573903d14a2c69c64dead95.tar.xz
AltCraft-40421159568518bb7573903d14a2c69c64dead95.tar.zst
AltCraft-40421159568518bb7573903d14a2c69c64dead95.zip
Diffstat (limited to 'src/Framebuffer.cpp')
-rw-r--r--src/Framebuffer.cpp129
1 files changed, 0 insertions, 129 deletions
diff --git a/src/Framebuffer.cpp b/src/Framebuffer.cpp
deleted file mode 100644
index d81b0e0..0000000
--- a/src/Framebuffer.cpp
+++ /dev/null
@@ -1,129 +0,0 @@
-#include "Framebuffer.hpp"
-#include "Shader.hpp"
-#include <string>
-#include "Utility.hpp"
-#include "AssetManager.hpp"
-#include <optick.h>
-
-const GLuint magic = 316784;
-GLuint quadVao = magic, quadVbo = magic;
-
-Framebuffer::Framebuffer(unsigned int width, unsigned int height, bool createDepthStencilBuffer) : width(width), height(height) {
- OPTICK_EVENT();
- if (quadVao == magic) {
- float quadVertices[] = {
- // positions // texCoords
- -1.0f, 1.0f, 0.0f, 1.0f,
- -1.0f, -1.0f, 0.0f, 0.0f,
- 1.0f, -1.0f, 1.0f, 0.0f,
-
- -1.0f, 1.0f, 0.0f, 1.0f,
- 1.0f, -1.0f, 1.0f, 0.0f,
- 1.0f, 1.0f, 1.0f, 1.0f
- };
-
- glGenVertexArrays(1, &quadVao);
- glGenBuffers(1, &quadVbo);
- glBindVertexArray(quadVao);
- glBindBuffer(GL_ARRAY_BUFFER, quadVbo);
- glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW);
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
- glEnableVertexAttribArray(1);
- glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float)));
- AssetManager::GetAsset<AssetShader>("/altcraft/shaders/fbo")->shader->Activate();
- AssetManager::GetAsset<AssetShader>("/altcraft/shaders/fbo")->shader->SetUniform("inputTexture", 1);
- glActiveTexture(GL_TEXTURE1);
- glCheckError();
- }
-
- glGenTextures(1, &texColor);
- glBindTexture(GL_TEXTURE_2D, texColor);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glCheckError();
-
- if (createDepthStencilBuffer) {
- glGenRenderbuffers(1, &rboDepthStencil);
- glBindRenderbuffer(GL_RENDERBUFFER, rboDepthStencil);
- glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
- }
-
- glGenFramebuffers(1, &fbo);
- glBindFramebuffer(GL_FRAMEBUFFER, fbo);
- glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texColor, 0);
- if(createDepthStencilBuffer)
- glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rboDepthStencil);
- glCheckError();
-
- GLenum framebufferStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
- if (framebufferStatus != GL_FRAMEBUFFER_COMPLETE)
- throw std::runtime_error("Failed to initialize framebuffer: " + std::to_string(framebufferStatus));
-}
-
-Framebuffer::~Framebuffer() {
- if (rboDepthStencil)
- glDeleteRenderbuffers(1, &rboDepthStencil);
- if (texColor)
- glDeleteTextures(1, &texColor);
-
- glDeleteFramebuffers(1, &fbo);
-}
-
-void Framebuffer::Activate() {
- OPTICK_EVENT();
- glViewport(0, 0, width, height);
- glBindFramebuffer(GL_FRAMEBUFFER, fbo);
-}
-
-void Framebuffer::RenderTo(Framebuffer &target) {
- OPTICK_EVENT();
- glBindFramebuffer(GL_FRAMEBUFFER, target.fbo);
- glViewport(0, 0, target.width, target.height);
- AssetManager::GetAsset<AssetShader>("/altcraft/shaders/fbo")->shader->Activate();
- glBindVertexArray(quadVao);
- glBindTexture(GL_TEXTURE_2D, texColor);
- glDrawArrays(GL_TRIANGLES, 0, 6);
-}
-
-void Framebuffer::Resize(unsigned int newWidth, unsigned int newHeight) {
- width = newWidth;
- height = newHeight;
- if (texColor) {
- glBindTexture(GL_TEXTURE_2D, texColor);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, newWidth, newHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
- }
- if (rboDepthStencil) {
- glBindRenderbuffer(GL_RENDERBUFFER, rboDepthStencil);
- glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
- }
-}
-
-Framebuffer &Framebuffer::GetDefault() {
- OPTICK_EVENT();
- static char fboDefaultData[sizeof(Framebuffer)];
- static Framebuffer *fboDefault = nullptr;
- if (fboDefault == nullptr) {
- fboDefault = reinterpret_cast<Framebuffer*>(fboDefaultData);
- fboDefault->fbo = 0;
- fboDefault->width = 1;
- fboDefault->height = 1;
- fboDefault->texColor = 0;
- fboDefault->rboDepthStencil = 0;
- }
- return *fboDefault;
-}
-
-void Framebuffer::Clear(bool color, bool depth, bool stencil) {
- OPTICK_EVENT();
- Activate();
- GLbitfield clearBits = 0;
- if (color)
- clearBits |= GL_COLOR_BUFFER_BIT;
- if (depth)
- clearBits |= GL_DEPTH_BUFFER_BIT;
- if (stencil)
- clearBits |= GL_STENCIL_BUFFER_BIT;
- glClear(clearBits);
-}