framebufferobject.cpp 7.52 KB
Newer Older
schultezub's avatar
schultezub committed
1
2
/**********************************************************************
 *                                                                    *
3
 * cgt - CAMP Graphics Toolbox, Copyright (C) 2012-2015               *
4
5
6
 *     Chair for Computer Aided Medical Procedures                    *
 *     Technische Universitaet Muenchen, Germany.                     *
 *     <http://campar.in.tum.de/>                                     *
schultezub's avatar
schultezub committed
7
 *                                                                    *
8
9
10
11
 * forked from tgt - Tiny Graphics Toolbox, Copyright (C) 2006-2011   *
 *     Visualization and Computer Graphics Group, Department of       *
 *     Computer Science, University of Muenster, Germany.             *
 *     <http://viscg.uni-muenster.de>                                 *
schultezub's avatar
schultezub committed
12
 *                                                                    *
13
 * This file is part of the cgt library. This library is free         *
schultezub's avatar
schultezub committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 * software; you can redistribute it and/or modify it under the terms *
 * of the GNU Lesser General Public License version 2.1 as published  *
 * by the Free Software Foundation.                                   *
 *                                                                    *
 * This library is distributed in the hope that it will be useful,    *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the       *
 * GNU Lesser General Public License for more details.                *
 *                                                                    *
 * You should have received a copy of the GNU Lesser General Public   *
 * License in the file "LICENSE.txt" along with this library.         *
 * If not, see <http://www.gnu.org/licenses/>.                        *
 *                                                                    *
 **********************************************************************/

29
30
#include "cgt/framebufferobject.h"
#include "cgt/logmanager.h"
schultezub's avatar
schultezub committed
31

32
33
#include <string.h> // memset

34
namespace cgt {
schultezub's avatar
schultezub committed
35

36
const std::string FramebufferObject::loggerCat_("cgt.FramebufferObject");
schultezub's avatar
schultezub committed
37
38
39

FramebufferObject::FramebufferObject()
  : id_(0)
40
  , numColorAttachments_(0)
schultezub's avatar
schultezub committed
41
{
42
    memset(attachments_, 0, sizeof(Texture*) * (CGT_FRAMEBUFFEROBJECT_MAX_SUPPORTED_COLOR_ATTACHMENTS+2));
schultezub's avatar
schultezub committed
43
44
45
46
47
    generateId();
}

FramebufferObject::~FramebufferObject()
{
48
    glDeleteFramebuffers(1, &id_);
schultezub's avatar
schultezub committed
49
50
51
52
}

void FramebufferObject::activate()
{
53
54
#if CGT_DEBUG
    if (cgt::getGlInt(GL_FRAMEBUFFER_BINDING) != 0)
55
56
57
        LWARNING("Binding a new FBO while another FBO is bound. Do you really want to do this?");
#endif

58
    glBindFramebuffer(GL_FRAMEBUFFER, id_);
59
    LGL_ERROR;
schultezub's avatar
schultezub committed
60
61
62
63
}

void FramebufferObject::deactivate()
{
64
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
65
    LGL_ERROR;
schultezub's avatar
schultezub committed
66
67
68
69
70
71
}

void FramebufferObject::attachTexture(Texture* texture, GLenum attachment, int mipLevel, int zSlice)
{
    switch(texture->getType()) {
        case GL_TEXTURE_1D:
72
            glFramebufferTexture1D( GL_FRAMEBUFFER, attachment, GL_TEXTURE_1D, texture->getId(), mipLevel );
schultezub's avatar
schultezub committed
73
74
            break;
        case GL_TEXTURE_3D:
75
            glFramebufferTexture3D( GL_FRAMEBUFFER, attachment, GL_TEXTURE_3D, texture->getId(), mipLevel, zSlice );
schultezub's avatar
schultezub committed
76
            break;
77
        case GL_TEXTURE_2D_ARRAY:
78
            glFramebufferTextureLayer( GL_FRAMEBUFFER, attachment, texture->getId(), mipLevel, zSlice );
schultezub's avatar
schultezub committed
79
80
            break;
        default: //GL_TEXTURE_2D, GL_TEXTURE_RECTANGLE
81
            glFramebufferTexture2D( GL_FRAMEBUFFER, attachment, texture->getType(), texture->getId(), mipLevel );
schultezub's avatar
schultezub committed
82
83
            break;
    }
84
    LGL_ERROR;
85
86
87

    size_t index = decodeAttachment(attachment);
    attachments_[index] = texture;
88
    if (index < CGT_FRAMEBUFFEROBJECT_MAX_SUPPORTED_COLOR_ATTACHMENTS)
89
        ++numColorAttachments_;
90
91
92
93

#ifdef CAMPVIS_DEBUG
    this->isComplete();
#endif
schultezub's avatar
schultezub committed
94
95
96
}

Texture* FramebufferObject::getTextureAtAttachment(GLenum attachment) {
97
    return attachments_[decodeAttachment(attachment)];
schultezub's avatar
schultezub committed
98
99
100
}

void FramebufferObject::detachTexture(GLenum attachment) {
101
102
    size_t index = decodeAttachment(attachment);
    if (attachments_[index] != 0) {
103
104
        switch (attachments_[index]->getType()) {
            case GL_TEXTURE_1D:
105
                glFramebufferTexture1D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_1D, 0, 0);
106
107
                break;
            case GL_TEXTURE_2D_ARRAY:
108
                glFramebufferTextureLayerEXT(GL_FRAMEBUFFER, attachment, 0, 0, 0);
109
110
                break;
            case GL_TEXTURE_3D:
111
                glFramebufferTexture3D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_3D, 0, 0, 0);
112
113
                break;
            default: // GL_TEXTURE_2D, GL_TEXTURE_RECTANGLE
114
                glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, 0, 0);
115
116
                break;
        }
117
        attachments_[index] = 0;
118
        LGL_ERROR;
119

120
        if (index < CGT_FRAMEBUFFEROBJECT_MAX_SUPPORTED_COLOR_ATTACHMENTS)
121
            --numColorAttachments_;
schultezub's avatar
schultezub committed
122
123
124
125
126
127
128
    }
    else {
        LWARNING("Trying to detach unknown texture!");
    }
}

void FramebufferObject::detachAll() {
129
    for (GLenum i = 0; i < CGT_FRAMEBUFFEROBJECT_MAX_SUPPORTED_COLOR_ATTACHMENTS; ++i) {
130
131
        if (colorAttachments_[i] != 0)
            detachTexture(GL_COLOR_ATTACHMENT0 + i);
schultezub's avatar
schultezub committed
132
    }
133
134
135
136
137
138
    if (depthAttachment_ != 0)
        detachTexture(GL_DEPTH_ATTACHMENT);
    if (stencilAttachment_ != 0)
        detachTexture(GL_STENCIL_ATTACHMENT);

    numColorAttachments_ = 0;
schultezub's avatar
schultezub committed
139
140
141
142
143
144
}

bool FramebufferObject::isComplete() const
{
  bool complete = false;

145
  GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
schultezub's avatar
schultezub committed
146
  switch(status) {
147
      case GL_FRAMEBUFFER_COMPLETE:
schultezub's avatar
schultezub committed
148
149
          complete = true;
          break;
150
151
      case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
          LERROR("GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT");
schultezub's avatar
schultezub committed
152
          break;
153
154
      case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
          LERROR("GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT");
schultezub's avatar
schultezub committed
155
          break;
156
157
      case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
          LERROR("GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE");
schultezub's avatar
schultezub committed
158
          break;
159
160
      case GL_FRAMEBUFFER_UNDEFINED:
          LERROR("GL_FRAMEBUFFER_UNDEFINED ");
schultezub's avatar
schultezub committed
161
          break;
162
163
      case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
          LERROR("GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER");
schultezub's avatar
schultezub committed
164
          break;
165
166
      case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
          LERROR("GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER");
schultezub's avatar
schultezub committed
167
          break;
168
169
      case GL_FRAMEBUFFER_UNSUPPORTED:
          LERROR("GL_FRAMEBUFFER_UNSUPPORTED");
schultezub's avatar
schultezub committed
170
171
172
173
174
175
176
177
178
179
180
181
182
          break;
      default:
          LERROR("Unknown error!");
  }
  return complete;
}

bool FramebufferObject::isActive() const {
    return ((getActiveObject() == id_) && (id_ != 0));
}

GLuint FramebufferObject::getActiveObject() {
    GLint fbo;
183
    glGetIntegerv (GL_FRAMEBUFFER_BINDING, &fbo);
schultezub's avatar
schultezub committed
184
185
186
187
188
    return static_cast<GLuint>(fbo);
}

GLuint FramebufferObject::generateId() {
    id_ = 0;
189
    glGenFramebuffers(1, &id_);
190
    LGL_ERROR;
schultezub's avatar
schultezub committed
191
192
193
    return id_;
}

194
Texture* const * FramebufferObject::getAttachments() const {
195
196
197
198
    return attachments_;
}

const Texture* FramebufferObject::getColorAttachment(size_t index /*= 0*/) const {
199
    cgtAssert(index < CGT_FRAMEBUFFEROBJECT_MAX_SUPPORTED_COLOR_ATTACHMENTS, "Index out of bounds!");
200
201
202
203
    return attachments_[index];
}

const Texture* FramebufferObject::getDepthAttachment() const {
204
    return attachments_[CGT_FRAMEBUFFEROBJECT_MAX_SUPPORTED_COLOR_ATTACHMENTS];
205
206
207
}

const Texture* FramebufferObject::getStencilAttachment() const {
208
    return attachments_[CGT_FRAMEBUFFEROBJECT_MAX_SUPPORTED_COLOR_ATTACHMENTS+1];
209
210
211
212
213
214
}

size_t FramebufferObject::getNumColorAttachments() const {
    return numColorAttachments_;
}

schultezub's avatar
schultezub committed
215
} // namespace