diff --git a/framebufferobject.cpp b/framebufferobject.cpp new file mode 100644 index 0000000..1cd6019 --- /dev/null +++ b/framebufferobject.cpp @@ -0,0 +1,79 @@ +#include "framebufferobject.h" + +FrameBufferObject::FrameBufferObject() +{ + glGenFramebuffers(1, &mFrameBufferObject); +} + +void FrameBufferObject::AttachColorBuffer(const char * bufferName, GLenum attachment, int width, int height) +{ + GLuint colorBuffer; + glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferObject); + glGenTextures(1, &colorBuffer); + glBindTexture(GL_TEXTURE_2D, colorBuffer); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glBindTexture(GL_TEXTURE_2D, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, colorBuffer, 0); + mDrawBuffers.push_back(attachment); + mBuffers.insert(std::pair(bufferName, colorBuffer)); + glBindFramebuffer(GL_FRAMEBUFFER, 0); +} + +void FrameBufferObject::AttachDepthBuffer(const char * bufferName, int width, int height) +{ + GLuint depthMap; + glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferObject); + glGenTextures(1, &depthMap); + glBindTexture(GL_TEXTURE_2D, depthMap); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, depthMap, 0); + glBindTexture(GL_TEXTURE_2D, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthMap, 0); + mBuffers.insert(std::pair(bufferName, depthMap)); + glBindFramebuffer(GL_FRAMEBUFFER, 0); +} + +void FrameBufferObject::Finish() +{ + int nCount = (int)mDrawBuffers.size(); + if (nCount > 0) { + GLenum *buffers = new GLenum[nCount]; + int i = 0; + while (i < nCount) { + buffers[i] = mDrawBuffers[i]; + i++; + } + glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferObject); + glDrawBuffers(nCount, buffers); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + } +} + +void FrameBufferObject::Bind() +{ + glGetIntegerv(GL_FRAMEBUFFER_BINDING, &mPrevFrameBuffer); + glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferObject); + glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); +} + +void FrameBufferObject::Unbind() +{ + glBindFramebuffer(GL_FRAMEBUFFER, mPrevFrameBuffer); +} + +GLuint FrameBufferObject::GetBuffer(const char * bufferName) +{ + auto iter = mBuffers.find(bufferName); + if (iter != mBuffers.end()) { + return iter->second; + } + return 0; +} + + diff --git a/framebufferobject.h b/framebufferobject.h new file mode 100644 index 0000000..5c79e80 --- /dev/null +++ b/framebufferobject.h @@ -0,0 +1,17 @@ +#pragma once +#include "ggl.h" +class FrameBufferObject { +public: + GLuint mFrameBufferObject; + GLint mPrevFrameBuffer; + std::map mBuffers; + std::vector mDrawBuffers; +public: + FrameBufferObject(); + void AttachColorBuffer(const char* bufferName, GLenum attachment, int width, int height); + void AttachDepthBuffer(const char* bufferName, int width, int height); + void Finish(); + void Bind(); + void Unbind(); + GLuint GetBuffer(const char* bufferName); +}; \ No newline at end of file diff --git a/renderFramework.vcxproj b/renderFramework.vcxproj index fd9d02f..75fe671 100644 --- a/renderFramework.vcxproj +++ b/renderFramework.vcxproj @@ -126,6 +126,7 @@ + @@ -134,6 +135,7 @@ + diff --git a/renderFramework.vcxproj.filters b/renderFramework.vcxproj.filters index 9a0550a..4bbf003 100644 --- a/renderFramework.vcxproj.filters +++ b/renderFramework.vcxproj.filters @@ -33,6 +33,9 @@ 源文件 + + 源文件 + @@ -53,5 +56,8 @@ 源文件 + + 源文件 + \ No newline at end of file