diff --git a/data/shaders/fs_simple.glsl b/data/shaders/fs_simple.glsl index 3e3e66c..0cb324f 100644 --- a/data/shaders/fs_simple.glsl +++ b/data/shaders/fs_simple.glsl @@ -1,6 +1,6 @@ #version 330 core -out vec3 color; +layout(location = 0) out vec3 color; void main() { color = vec3(1, 0, 0); diff --git a/src/modules/RenderModule.cc b/src/modules/RenderModule.cc index f36f22a..212c180 100644 --- a/src/modules/RenderModule.cc +++ b/src/modules/RenderModule.cc @@ -122,12 +122,24 @@ glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data mProgram = RenderProgram("data/shaders/vs_simple.glsl", "data/shaders/fs_simple.glsl"); bool load_result = mProgram.Load(); assert(load_result); + + mRenderTarget = RenderTarget (width, height, RenderTarget::EnableColor | RenderTarget::EnableDepth); } void Renderer::Shutdown() { glDeleteVertexArrays(1, &mMesh.mVertexArrayId); } void Renderer::RenderGl() { + // enable the render target + glBindFramebuffer(GL_FRAMEBUFFER, mRenderTarget.mFrameBufferId); + GLenum DrawBuffers[1] = { GL_COLOR_ATTACHMENT0 }; + glDrawBuffers(1, DrawBuffers); + + if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { + gLog ("Cannot render: frame buffer invalid!"); + } + + // clear color and depth glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(mProgram.mProgramId); diff --git a/src/modules/RenderModule.h b/src/modules/RenderModule.h index 684c534..d20d44c 100644 --- a/src/modules/RenderModule.h +++ b/src/modules/RenderModule.h @@ -114,6 +114,7 @@ struct Renderer { std::vector cameras; Mesh mMesh; RenderProgram mProgram; + RenderTarget mRenderTarget; Renderer() : initialized(false), diff --git a/src/modules/RenderUtils.cc b/src/modules/RenderUtils.cc index 6872fb7..6234dff 100644 --- a/src/modules/RenderUtils.cc +++ b/src/modules/RenderUtils.cc @@ -10,6 +10,10 @@ using namespace SimpleMath; +// +// RenderProgram +// + RenderProgram::~RenderProgram() { if (mProgramId > 0) glDeleteProgram(mProgramId); @@ -109,4 +113,35 @@ bool RenderProgram::Load() { return true; } +// +// RenderTarget +// +RenderTarget::RenderTarget(int width, int height, int flags) { + glGenFramebuffers(1, &mFrameBufferId); + glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferId); + + if (flags & EnableColor) { + glGenTextures(1, &mColorTexture); + glBindTexture(GL_TEXTURE_2D, mColorTexture); + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + + glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, mColorTexture, 0); + } + + if (flags & EnableDepth) { + glGenRenderbuffers(1, &mDepthBuffer); + glBindRenderbuffer(GL_RENDERBUFFER, mDepthBuffer); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, mDepthBuffer); + } +} + +RenderTarget::~RenderTarget() { + // TODO: cleanup +} + diff --git a/src/modules/RenderUtils.h b/src/modules/RenderUtils.h index b1a397e..d645fcb 100644 --- a/src/modules/RenderUtils.h +++ b/src/modules/RenderUtils.h @@ -144,5 +144,19 @@ struct RenderProgram { bool Load(); }; +struct RenderTarget { + GLuint mFrameBufferId = -1; + GLuint mColorTexture = -1; + GLuint mDepthBuffer = -1; + + typedef enum { + EnableColor = 1, + EnableDepth = 2 + } Flags; + + RenderTarget() {}; + RenderTarget(int width, int height, int flags); + ~RenderTarget(); +}; #endif