00001 #include "GLFramebuffer.h"
00002
00003 #include <QDebug>
00004
00005 GLFramebuffer::GLFramebuffer()
00006 {
00007 buffer = 0;
00008 }
00009
00010 GLFramebuffer::~GLFramebuffer()
00011 {
00012 free();
00013 }
00014
00015 GLuint GLFramebuffer::getBuffer()
00016 {
00017 return buffer;
00018 }
00019
00020 GLuint GLFramebuffer::getWidth()
00021 {
00022 return width;
00023 }
00024
00025 GLuint GLFramebuffer::getHeight()
00026 {
00027 return height;
00028 }
00029
00030 GLTexture* GLFramebuffer::createTexture()
00031 {
00032 textures.push_back(GLTexture());
00033 return &textures.back();
00034 }
00035
00036 GLTexture* GLFramebuffer::getTexture(int index)
00037 {
00038 return &(textures[index]);
00039 }
00040
00041 void GLFramebuffer::create(GLuint width, GLuint height)
00042 {
00043 free();
00044
00045 if(textures.size() != 0)
00046 {
00047 this->width = width;
00048 this->height = height;
00049
00050 GLenum glError;
00051
00052
00053 GLint csBuffer;
00054 glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &csBuffer);
00055
00056 glGenFramebuffersEXT(1, &buffer);
00057 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, buffer);
00058
00059 glError = glGetError();
00060 if(glError != GL_NO_ERROR)
00061 {
00062 qDebug() << "create GLFramebuffer error / generate buffer: " << (char*)gluErrorString(glError);
00063 }
00064
00065 GLenum *bufs = new GLenum[textures.size()];
00066
00067 for(unsigned int i = 0;i < textures.size();++i)
00068 {
00069 textures[i].create(width, height);
00070 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + i, GL_TEXTURE_2D, textures[i].getTexture(), 0);
00071
00072 glError = glGetError();
00073 if(glError != GL_NO_ERROR)
00074 {
00075 qDebug() << "create GLFramebuffer error / attach: " << (char*)gluErrorString(glError);
00076 }
00077
00078 bufs[i] = GL_COLOR_ATTACHMENT0_EXT + i;
00079 }
00080
00081 glDrawBuffers(textures.size(), bufs);
00082 glError = glGetError();
00083 if(glError != GL_NO_ERROR)
00084 {
00085 qDebug() << "create GLFramebuffer error / glDrawBuffers: " << (char*)gluErrorString(glError);
00086 }
00087
00088 delete [] bufs;
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, csBuffer);
00112
00113 glError = glGetError();
00114 if(glError != GL_NO_ERROR)
00115 {
00116 qDebug() << "create GLFramebuffer error / unbind: " << (char*)gluErrorString(glError);
00117 }
00118 }
00119 }
00120
00121 void GLFramebuffer::free()
00122 {
00123 if(buffer != 0)
00124 {
00125 glDeleteFramebuffersEXT(1, &buffer);
00126 buffer = 0;
00127
00128 for(unsigned int i = 0;i < textures.size();++i)
00129 {
00130 textures[i].free();
00131 }
00132
00133 GLenum glError = glGetError();
00134 if(glError != GL_NO_ERROR)
00135 {
00136 qDebug() << "free FrameTextureBuffer error: " << (char*)gluErrorString(glError);
00137 }
00138 }
00139 }