1#include "FrameBuffer.h" 2 3FrameBuffer::FrameBuffer() 4{ 5 Reset(); 6} 7 8FrameBuffer::~FrameBuffer() { 9} 10 11void FrameBuffer::Reset() { 12 mFrameBufferName = -1; 13 mTextureName = -1; 14 mWidth = 0; 15 mHeight = 0; 16 mFormat = -1; 17} 18 19bool FrameBuffer::InitializeGLContext() { 20 Reset(); 21 return CreateBuffers(); 22} 23 24bool FrameBuffer::Init(int width, int height, GLenum format) { 25 if (mFrameBufferName == (GLuint)-1) { 26 if (!CreateBuffers()) { 27 return false; 28 } 29 } 30 glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferName); 31 glBindTexture(GL_TEXTURE_2D, mTextureName); 32 33 glTexImage2D(GL_TEXTURE_2D, 34 0, 35 format, 36 width, 37 height, 38 0, 39 format, 40 GL_UNSIGNED_BYTE, 41 NULL); 42 if (!checkGlError("bind/teximage")) { 43 return false; 44 } 45 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 46 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 47 // This is necessary to work with user-generated frame buffers with 48 // dimensions that are NOT powers of 2. 49 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 50 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 51 52 // Attach texture to frame buffer. 53 glFramebufferTexture2D(GL_FRAMEBUFFER, 54 GL_COLOR_ATTACHMENT0, 55 GL_TEXTURE_2D, 56 mTextureName, 57 0); 58 checkFramebufferStatus("FrameBuffer.cpp"); 59 checkGlError("framebuffertexture2d"); 60 61 if (!checkGlError("texture setup")) { 62 return false; 63 } 64 mWidth = width; 65 mHeight = height; 66 mFormat = format; 67 glBindFramebuffer(GL_FRAMEBUFFER, 0); 68 return true; 69} 70 71bool FrameBuffer::CreateBuffers() { 72 glGenFramebuffers(1, &mFrameBufferName); 73 glGenTextures(1, &mTextureName); 74 if (!checkGlError("texture generation")) { 75 return false; 76 } 77 return true; 78} 79 80GLuint FrameBuffer::GetTextureName() const { 81 return mTextureName; 82} 83 84GLuint FrameBuffer::GetFrameBufferName() const { 85 return mFrameBufferName; 86} 87 88GLenum FrameBuffer::GetFormat() const { 89 return mFormat; 90} 91 92int FrameBuffer::GetWidth() const { 93 return mWidth; 94} 95 96int FrameBuffer::GetHeight() const { 97 return mHeight; 98} 99