You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.6 KiB
72 lines
2.6 KiB
#include "framebufferobject.h"
|
|
|
|
FrameBufferObject::FrameBufferObject()
|
|
{
|
|
glGenFramebuffers(1, &mFrameBufferObject);
|
|
}
|
|
void FrameBufferObject::AttachColorBuffer(const char*bufferName, GLenum attachment, int width, int height) {
|
|
|
|
//在显存中申请一块空间,并置空
|
|
GLuint colorBuffer;
|
|
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);
|
|
|
|
//把上面申请的空间和frame buffer 绑定
|
|
glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferObject);
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, colorBuffer, 0);
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
mDrawBuffers.push_back(attachment);
|
|
mBuffers.insert(std::pair<std::string, GLuint>(bufferName, colorBuffer));
|
|
}
|
|
void FrameBufferObject::AttachDepthBuffer(const char*bufferName, int width, int height) {
|
|
GLuint depthMap;
|
|
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, GL_FLOAT, NULL);
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferObject);
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthMap, 0);
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
mBuffers.insert(std::pair<std::string, GLuint>(bufferName, depthMap));
|
|
}
|
|
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);//绑定前记录一下buffer,为解绑返回这个buffer做准备
|
|
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;
|
|
}
|