From e2295392b8e5ff42f2dca92aef5bfe70c6daf2d2 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Mon, 11 Jun 2012 21:21:46 +0000 Subject: [PATCH] gpu: first FrameBuffer GPU class. --- src/Makefile.am | 1 + src/core.h | 1 + src/gpu/framebuffer.cpp | 155 ++++++++++++++++++++++++++++++++++++++++ src/gpu/framebuffer.h | 44 ++++++++++++ src/gpu/indexbuffer.cpp | 2 +- 5 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 src/gpu/framebuffer.cpp create mode 100644 src/gpu/framebuffer.h diff --git a/src/Makefile.am b/src/Makefile.am index ee5656a7..a4c0af7c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -35,6 +35,7 @@ liblol_a_SOURCES = \ gpu/shader.cpp gpu/shader.h \ gpu/indexbuffer.cpp gpu/indexbuffer.h \ gpu/vertexbuffer.cpp gpu/vertexbuffer.h \ + gpu/framebuffer.cpp gpu/framebuffer.h \ \ image/image.cpp image/image.h image/image-private.h \ image/codec/gdiplus-image.cpp \ diff --git a/src/core.h b/src/core.h index 6c43a420..389de12d 100644 --- a/src/core.h +++ b/src/core.h @@ -109,6 +109,7 @@ static inline int isnan(float f) #include "gpu/shader.h" #include "gpu/indexbuffer.h" #include "gpu/vertexbuffer.h" +#include "gpu/framebuffer.h" #include "image/image.h" #include "application/application.h" diff --git a/src/gpu/framebuffer.cpp b/src/gpu/framebuffer.cpp new file mode 100644 index 00000000..70b58b88 --- /dev/null +++ b/src/gpu/framebuffer.cpp @@ -0,0 +1,155 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2012 Sam Hocevar +// This program is free software; you can redistribute it and/or +// modify it under the terms of the Do What The Fuck You Want To +// Public License, Version 2, as published by Sam Hocevar. See +// http://sam.zoy.org/projects/COPYING.WTFPL for more details. +// + +#if defined HAVE_CONFIG_H +# include "config.h" +#endif + +#include "core.h" +#include "lolgl.h" + +#if defined _WIN32 && defined USE_D3D9 +# define FAR +# define NEAR +# include +#endif + +using namespace std; + +#if defined USE_D3D9 +extern IDirect3DDevice9 *g_d3ddevice; +#elif defined _XBOX +extern D3DDevice *g_d3ddevice; +#endif + +namespace lol +{ + +// +// The FrameBufferData class +// ------------------------- +// + +class FrameBufferData +{ + friend class FrameBuffer; + + ivec2 m_size; + +#if defined USE_D3D9 +#elif defined _XBOX +#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ + GLuint m_fbo, m_texture, m_depth; +#endif +}; + +// +// The FrameBuffer class +// ---------------------- +// + +FrameBuffer::FrameBuffer(ivec2 size) + : m_data(new FrameBufferData) +{ + m_data->m_size = size; +#if defined USE_D3D9 || defined _XBOX +#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ + GLenum format = GL_RGBA8; + GLenum depth = GL_DEPTH_COMPONENT; + GLenum wrapmode = GL_REPEAT; + GLenum filtering = GL_NEAREST; + + glGenFramebuffers(1, &m_data->m_fbo); + glBindFramebuffer(GL_FRAMEBUFFER, m_data->m_fbo); + + glGenTextures(1, &m_data->m_texture); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, m_data->m_texture); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (GLenum)wrapmode); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (GLenum)wrapmode); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (GLenum)filtering); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (GLenum)filtering); + glTexImage2D(GL_TEXTURE_2D, 0, (GLenum)format, size.x, size.y, 0, + GL_BGRA, GL_UNSIGNED_BYTE, NULL); + + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, + GL_TEXTURE_2D, m_data->m_texture, 0); + m_data->m_depth = GL_INVALID_INDEX; + if (depth != GL_INVALID_INDEX) + { + glGenRenderbuffers(1, &m_data->m_depth); + glBindRenderbuffer(GL_RENDERBUFFER, m_data->m_depth); + glRenderbufferStorage(GL_RENDERBUFFER, depth, size.x, size.y); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, + GL_RENDERBUFFER, m_data->m_depth); + } + glCheckFramebufferStatus(GL_FRAMEBUFFER); + + Unbind(); +#endif +} + +FrameBuffer::~FrameBuffer() +{ +#if defined USE_D3D9 || defined _XBOX +#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ + glDeleteFramebuffers(1, &m_data->m_fbo); + glDeleteTextures(1, &m_data->m_texture); + if (m_data->m_depth != GL_INVALID_INDEX) + glDeleteRenderbuffers(1, &m_data->m_depth); +#endif + delete m_data; +} + +int FrameBuffer::GetTexture() const +{ +#if defined USE_D3D9 || defined _XBOX +#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ + return m_data->m_texture; +#endif +} + +void FrameBuffer::Clear(vec4 color) +{ +#if defined USE_D3D9 || defined _XBOX +#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ + glClearColor(color.r, color.g, color.b, color.a); + glClear(GL_COLOR_BUFFER_BIT); +#endif +} + +void FrameBuffer::Clear(vec4 color, float depth) +{ +#if defined USE_D3D9 || defined _XBOX +#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ + glClearColor(color.r, color.g, color.b, color.a); + //glClearDepthf(depth); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); +#endif +} + +void FrameBuffer::Bind() +{ +#if defined USE_D3D9 || defined _XBOX +#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ + glBindFramebuffer(GL_FRAMEBUFFER, m_data->m_fbo); +#endif +} + +void FrameBuffer::Unbind() +{ +#if defined USE_D3D9 || defined _XBOX +#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ + glBindFramebuffer(GL_FRAMEBUFFER, NULL); +#endif +} + +} /* namespace lol */ + diff --git a/src/gpu/framebuffer.h b/src/gpu/framebuffer.h new file mode 100644 index 00000000..0c39fcbb --- /dev/null +++ b/src/gpu/framebuffer.h @@ -0,0 +1,44 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2012 Sam Hocevar +// This program is free software; you can redistribute it and/or +// modify it under the terms of the Do What The Fuck You Want To +// Public License, Version 2, as published by Sam Hocevar. See +// http://sam.zoy.org/projects/COPYING.WTFPL for more details. +// + +// +// The FrameBuffer class +// --------------------- +// + +#if !defined __LOL_FRAMEBUFFER_H__ +#define __LOL_FRAMEBUFFER_H__ + +#include + +namespace lol +{ + +class FrameBuffer +{ +public: + FrameBuffer(ivec2 size); + ~FrameBuffer(); + + int GetTexture() const; + void Clear(vec4 color); + void Clear(vec4 color, float depth); + + void Bind(); + void Unbind(); + +private: + class FrameBufferData *m_data; +}; + +} /* namespace lol */ + +#endif // __LOL_FRAMEBUFFER_H__ + diff --git a/src/gpu/indexbuffer.cpp b/src/gpu/indexbuffer.cpp index b950128d..bc68167e 100644 --- a/src/gpu/indexbuffer.cpp +++ b/src/gpu/indexbuffer.cpp @@ -34,7 +34,7 @@ namespace lol // // The IndexBufferData class -// -------------------------- +// ------------------------- // class IndexBufferData