summaryrefslogtreecommitdiffstats
path: root/jni_mosaic/feature_mos/src/mosaic_renderer/FrameBuffer.cpp
blob: a956f23b77da5dd1ef129a6656b2d66260415152 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "FrameBuffer.h"

FrameBuffer::FrameBuffer()
{
    Reset();
}

FrameBuffer::~FrameBuffer() {
}

void FrameBuffer::Reset() {
    mFrameBufferName = -1;
    mTextureName = -1;
    mWidth = 0;
    mHeight = 0;
    mFormat = -1;
}

bool FrameBuffer::InitializeGLContext() {
    Reset();
    return CreateBuffers();
}

bool FrameBuffer::Init(int width, int height, GLenum format) {
    if (mFrameBufferName == (GLuint)-1) {
        if (!CreateBuffers()) {
            return false;
        }
    }
    glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferName);
    glBindTexture(GL_TEXTURE_2D, mTextureName);

    glTexImage2D(GL_TEXTURE_2D,
                 0,
                 format,
                 width,
                 height,
                 0,
                 format,
                 GL_UNSIGNED_BYTE,
                 NULL);
    if (!checkGlError("bind/teximage")) {
        return false;
    }
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    // This is necessary to work with user-generated frame buffers with
    // dimensions that are NOT powers of 2.
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    // Attach texture to frame buffer.
    glFramebufferTexture2D(GL_FRAMEBUFFER,
                           GL_COLOR_ATTACHMENT0,
                           GL_TEXTURE_2D,
                           mTextureName,
                           0);
    checkFramebufferStatus("FrameBuffer.cpp");
    checkGlError("framebuffertexture2d");

    if (!checkGlError("texture setup")) {
        return false;
    }
    mWidth = width;
    mHeight = height;
    mFormat = format;
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    return true;
}

bool FrameBuffer::CreateBuffers() {
    glGenFramebuffers(1, &mFrameBufferName);
    glGenTextures(1, &mTextureName);
    if (!checkGlError("texture generation")) {
        return false;
    }
    return true;
}

GLuint FrameBuffer::GetTextureName() const {
    return mTextureName;
}

GLuint FrameBuffer::GetFrameBufferName() const {
    return mFrameBufferName;
}

GLenum FrameBuffer::GetFormat() const {
    return mFormat;
}

int FrameBuffer::GetWidth() const {
    return mWidth;
}

int FrameBuffer::GetHeight() const {
    return mHeight;
}