Home | History | Annotate | Download | only in mosaic_renderer
      1 #include "FrameBuffer.h"
      2 
      3 FrameBuffer::FrameBuffer()
      4 {
      5     Reset();
      6 }
      7 
      8 FrameBuffer::~FrameBuffer() {
      9 }
     10 
     11 void FrameBuffer::Reset() {
     12     mFrameBufferName = -1;
     13     mTextureName = -1;
     14     mWidth = 0;
     15     mHeight = 0;
     16     mFormat = -1;
     17 }
     18 
     19 bool FrameBuffer::InitializeGLContext() {
     20     Reset();
     21     return CreateBuffers();
     22 }
     23 
     24 bool 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 
     59     if (!checkGlError("texture setup")) {
     60         return false;
     61     }
     62     mWidth = width;
     63     mHeight = height;
     64     mFormat = format;
     65     glBindFramebuffer(GL_FRAMEBUFFER, 0);
     66     return true;
     67 }
     68 
     69 bool FrameBuffer::CreateBuffers() {
     70     glGenFramebuffers(1, &mFrameBufferName);
     71     glGenTextures(1, &mTextureName);
     72     if (!checkGlError("texture generation")) {
     73         return false;
     74     }
     75     return true;
     76 }
     77 
     78 GLuint FrameBuffer::GetTextureName() const {
     79     return mTextureName;
     80 }
     81 
     82 GLuint FrameBuffer::GetFrameBufferName() const {
     83     return mFrameBufferName;
     84 }
     85 
     86 GLenum FrameBuffer::GetFormat() const {
     87     return mFormat;
     88 }
     89 
     90 int FrameBuffer::GetWidth() const {
     91     return mWidth;
     92 }
     93 
     94 int FrameBuffer::GetHeight() const {
     95     return mHeight;
     96 }
     97 
     98 
     99 
    100