From 88fc14e2bfed72dbc6a653eeb96a41832eae06d1 Mon Sep 17 00:00:00 2001 From: Harry Date: Tue, 21 Mar 2017 20:30:37 +0000 Subject: [PATCH] Added a simple render-to-texture ability via FrameBuffer --- res/runtime/resources.data | 4 +++- src/texture.cpp | 49 ++++++++++++++++++++++++++++++++++---- src/texture.hpp | 12 +++++++++- 3 files changed, 58 insertions(+), 7 deletions(-) diff --git a/res/runtime/resources.data b/res/runtime/resources.data index 2aea7edb82..3b65708a8a 100644 --- a/res/runtime/resources.data +++ b/res/runtime/resources.data @@ -79,4 +79,6 @@ speed: 2.5 -played: 117352 + + +played: 117375 diff --git a/src/texture.cpp b/src/texture.cpp index 30e0317f75..08a0e1d052 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -1,5 +1,49 @@ #include "texture.hpp" +FrameBuffer::FrameBuffer(unsigned int width, unsigned int height): width(width), height(height), fbHandle(0) +{ + glGenFramebuffers(1, &this->fbHandle); + glBindFramebuffer(GL_FRAMEBUFFER, this->fbHandle); + + glGenTextures(1, &this->texHandle); + glBindTexture(GL_TEXTURE_2D, this->texHandle); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, this->width, this->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); + + //Filtering + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + + glGenRenderbuffers(1, &this->depthRenderBufferHandle); + glBindRenderbuffer(GL_RENDERBUFFER, this->depthRenderBufferHandle); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, this->width, this->height); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, this->depthRenderBufferHandle); + + //Configure framebuffer + glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, this->texHandle, 0); + GLenum drawBuffers[1] = {GL_COLOR_ATTACHMENT0}; + glDrawBuffers(1, drawBuffers); + + if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) + { + //problim + } +} + +void FrameBuffer::setRenderTarget() const +{ + glBindFramebuffer(GL_FRAMEBUFFER, this->fbHandle); + glViewport(0, 0, this->width, this->height); +} + +void FrameBuffer::bind(unsigned int id) const +{ + assert(id >= 0 && id <= 31); + // this sets which texture we want to bind (id can be from 0 to 31) + // GLTEXTURE0 is actually a number, so we can add the id instead of a massive switch statement + glActiveTexture(GL_TEXTURE0 + id); + glBindTexture(GL_TEXTURE_2D, this->texHandle); +} + unsigned char* Texture::loadTexture() { return stbi_load((this->filename).c_str(), &(this->width), &(this->height), &(this->comps), 4); @@ -66,11 +110,6 @@ void Texture::bind(GLuint shaderProgram, unsigned int id) glUniform1i(this->textureID, id); } -void Texture::setRenderTarget() const -{ - -} - std::string Texture::getFileName() const { return this->filename; diff --git a/src/texture.hpp b/src/texture.hpp index cf67474f6d..230c06fc56 100644 --- a/src/texture.hpp +++ b/src/texture.hpp @@ -12,13 +12,23 @@ #endif #include "glew.h" +class FrameBuffer +{ +public: + FrameBuffer(unsigned int width = 256, unsigned int height = 256); + void setRenderTarget() const; + void bind(unsigned int id) const; +private: + unsigned int width, height; + GLuint fbHandle, texHandle, depthRenderBufferHandle; +}; + class Texture { public: Texture(std::string filename = "./res/textures/undefined.jpg"); ~Texture(); void bind(GLuint shaderProgram, unsigned int id); - void setRenderTarget() const; std::string getFileName() const; static std::shared_ptr getFromLink(std::string textureLink, std::vector> allTextures); protected: