Browse Source

framebuffer 类封装

master
blobt 4 years ago
parent
commit
6f3e688fa3
  1. 79
      framebufferobject.cpp
  2. 17
      framebufferobject.h
  3. 2
      renderFramework.vcxproj
  4. 6
      renderFramework.vcxproj.filters

79
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<std::string, GLuint>(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<std::string, GLuint>(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;
}

17
framebufferobject.h

@ -0,0 +1,17 @@
#pragma once
#include "ggl.h"
class FrameBufferObject {
public:
GLuint mFrameBufferObject;
GLint mPrevFrameBuffer;
std::map<std::string, GLuint> mBuffers;
std::vector<GLenum> 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);
};

2
renderFramework.vcxproj

@ -126,6 +126,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="framebufferobject.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="model.cpp" />
<ClCompile Include="scene.cpp" />
@ -134,6 +135,7 @@
<ClCompile Include="vertexbuffer.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="framebufferobject.h" />
<ClInclude Include="ggl.h" />
<ClInclude Include="model.h" />
<ClInclude Include="scene.h" />

6
renderFramework.vcxproj.filters

@ -33,6 +33,9 @@
<ClCompile Include="model.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="framebufferobject.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="ggl.h">
@ -53,5 +56,8 @@
<ClInclude Include="model.h">
<Filter>源文件</Filter>
</ClInclude>
<ClInclude Include="framebufferobject.h">
<Filter>源文件</Filter>
</ClInclude>
</ItemGroup>
</Project>
Loading…
Cancel
Save