Skip to content

Commit

Permalink
Added a simple render-to-texture ability via FrameBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
harrand committed Mar 21, 2017
1 parent c5ed6f9 commit 88fc14e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
4 changes: 3 additions & 1 deletion res/runtime/resources.data
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,6 @@ speed: 2.5



played: 117352


played: 117375
49 changes: 44 additions & 5 deletions src/texture.cpp
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 11 additions & 1 deletion src/texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Texture> getFromLink(std::string textureLink, std::vector<std::shared_ptr<Texture>> allTextures);
protected:
Expand Down

0 comments on commit 88fc14e

Please sign in to comment.