graphics

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit d391a6083cbaaae1caf07e7af514a501093f473a
parent b695b9d6b2b2da872f6cd0c1ec1ef36f0ac5b314
Author: Brian Swetland <swetland@frotz.net>
Date:   Sun, 15 Sep 2013 05:20:47 -0700

core: add FrameBuffer object for render-to-texture

Diffstat:
Mcommon/buffers.cc | 36++++++++++++++++++++++++++++++++++++
Mcommon/core.h | 19+++++++++++++++++++
Mcommon/module.mk | 1+
3 files changed, 56 insertions(+), 0 deletions(-)

diff --git a/common/buffers.cc b/common/buffers.cc @@ -149,3 +149,39 @@ int Texture2D::createRGBA(unsigned w, unsigned h) { height = h; return 0; } + +void FrameBuffer::init(unsigned w, unsigned h, unsigned _depth) { + glGenTextures(1, &txid); + glActiveTexture(GL_TEXTURE0 + 15); + glBindTexture(GL_TEXTURE_RECTANGLE, txid); + glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_BASE_LEVEL, 0); + glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAX_LEVEL, 0); + glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + + if (_depth) { + glGenRenderbuffers(1, &depth); + glBindRenderbuffer(GL_RENDERBUFFER, depth); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, w, h); + glBindRenderbuffer(GL_RENDERBUFFER, 0); + } + + glGenFramebuffers(1, &id); + glBindFramebuffer(GL_FRAMEBUFFER, id); + + /* color buffer */ + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, + GL_TEXTURE_RECTANGLE, txid, 0); + + /* depth buffer */ + if (_depth) { + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, + GL_RENDERBUFFER, depth); + } + + glBindFramebuffer(GL_FRAMEBUFFER, 0); + + width = w; + height = h; +} diff --git a/common/core.h b/common/core.h @@ -141,5 +141,24 @@ struct VertexAttributes { void use(void) { glBindVertexArray(vao); }; }; +class FrameBuffer { +public: + FrameBuffer() : id(0), txid(0), depth(0) {} + void init(unsigned w, unsigned h, unsigned _depth = 1); + void useTexture(unsigned index) { + glActiveTexture(GL_TEXTURE0 + index); + glBindTexture(GL_TEXTURE_RECTANGLE, txid); + } + void use(void) { + glBindFramebuffer(GL_FRAMEBUFFER, id); + glViewport(0, 0, width, height); + } +private: + u32 id; + u32 txid; + u32 depth; + u32 width; + u32 height; +}; #endif diff --git a/common/module.mk b/common/module.mk @@ -13,6 +13,7 @@ M_OBJS += savepng.o M_OBJS += simplexnoise.o M_OBJS += textgrid.o M_OBJS += texturefont.o +M_OBJS += Renderable.o M_OBJS += Model.o M_OBJS += Effect.o M_LIBS := common