FrameBufferObject.cc 6.85 KB
Newer Older
Robert Menzel's avatar
Robert Menzel committed
1
2
3
4
5
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University         //
// All rights reserved.                                                       //
////////////////////////////////////////////////////////////////////////////////

6
#include <ACGL/OpenGL/Objects/FrameBufferObject.hh>
Robert Menzel's avatar
Robert Menzel committed
7

8
using namespace ACGL;
Robert Menzel's avatar
Robert Menzel committed
9
using namespace ACGL::OpenGL;
Robert Menzel's avatar
Robert Menzel committed
10

11
int_t FrameBufferObject::getColorAttachmentIndexByName(const std::string& _name) const
12
13
14
15
16
17
18
19
20
{
     for(AttachmentVec::size_type i = 0; i < mColorAttachments.size(); i++)
     {
         if(mColorAttachments[i].name == _name)
             return i;
     }

     return -1;
 }
21

Robert Menzel's avatar
Robert Menzel committed
22
23
bool FrameBufferObject::isFrameBufferObjectComplete() const
{
Janis Born's avatar
Janis Born committed
24
    bind();
Robert Menzel's avatar
Robert Menzel committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
    if (status != GL_FRAMEBUFFER_COMPLETE)
    {
        Utils::error() << "Failed to make complete FrameBufferObject object: ";
        if (status == GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT) {
            Utils::error() << "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT";
        } else if (status == GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT) {
            Utils::error() << "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT";
        } else if (status == GL_FRAMEBUFFER_UNSUPPORTED) {
            Utils::error() << "GL_FRAMEBUFFER_UNSUPPORTED";
        } else if (status == GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE) {
            Utils::error() << "GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE";
        } else {
            Utils::error() << status;
        }
        Utils::error() << std::endl;
        return false;
    }
    return true;
}

46
void FrameBufferObject::validate(void) const
47
{
Robert Menzel's avatar
Robert Menzel committed
48
49
50
51
    // if OpenGL says were ok, return
    if (isFrameBufferObjectComplete()) return;

    // the above call will create some output, but let's try to get more infos:
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
    if(mColorAttachments.size() > 0)
    {
        int width  = -1;
        int height = -1;

        if(mColorAttachments[0].texture)
        {
            width  = mColorAttachments[0].texture->getWidth();
            height = mColorAttachments[0].texture->getHeight();
        }
        else
        {
            width  = mColorAttachments[0].renderBuffer->getWidth();
            height = mColorAttachments[0].renderBuffer->getHeight();
        }

        for(AttachmentVec::size_type k = 0; k < mColorAttachments.size(); k++)
        {
            bool fail = false;

            if(mColorAttachments[k].texture)
                fail = (mColorAttachments[k].texture->getWidth() != width || mColorAttachments[k].texture->getHeight() != height);
            else //otherwise its a RenderBuffer
                fail = (mColorAttachments[k].renderBuffer->getWidth() != width || mColorAttachments[k].renderBuffer->getHeight() != height);

            if(fail)
78
                Utils::error() << "FrameBufferObject validation failed: Color attachment "<< k << " has different size." << std::endl;
79
80
81
        }
    }
    else
82
        Utils::error() << "FrameBufferObject validation failed: No color attachments."<< std::endl;
83
}
84

Robert Menzel's avatar
Robert Menzel committed
85
bool FrameBufferObject::attachColorAttachment( const Attachment &_attachment )
86
{
Robert Menzel's avatar
Robert Menzel committed
87
    int realLocation = -1;
88

Robert Menzel's avatar
Robert Menzel committed
89
90
91
92
93
94
95
96
97
98
99
100
    for (unsigned int i = 0; i < mColorAttachments.size(); ++i) {
        if (mColorAttachments[i].name == _attachment.name) {
            // replace this attachment
            mColorAttachments[i] = _attachment;
            realLocation = i;
        }
    }
    if (realLocation == -1) {
        // it's a new attachment
        realLocation = mColorAttachments.size();
        mColorAttachments.push_back(_attachment);
    }
101

Robert Menzel's avatar
Robert Menzel committed
102
103
104
105
106
107
108
109
110
    // attach it to the OpenGL object:
    bind();
    openGLCriticalErrorOccured();
    if (_attachment.renderBuffer) {
        // it's a renderBuffer
        glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + realLocation, GL_RENDERBUFFER, _attachment.renderBuffer->getObjectName() );
    } else {
        // it's a texture
        glFramebufferTexture2D(    GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + realLocation, _attachment.texture->getTarget(), _attachment.texture->getObjectName(), 0 );
Robert Menzel's avatar
Robert Menzel committed
111
        //Utils::debug() << "glFramebufferTexture2D( " << _attachment.name << " to " << realLocation <<" )" << std::endl;
Robert Menzel's avatar
Robert Menzel committed
112
113
    }
    if (openGLCommonErrorOccured())
114
    {
Robert Menzel's avatar
Robert Menzel committed
115
116
117
        Utils::error() << "Attaching of render target to the FBO failed" << std::endl;
        return false;
    }
118

Robert Menzel's avatar
Robert Menzel committed
119
120
121
    remapAttachments();
    return true;
}
122

Robert Menzel's avatar
Robert Menzel committed
123
124
void FrameBufferObject::remapAttachments()
{
Janis Born's avatar
Janis Born committed
125
126
127
128
    GLint maxColorBuffers;
    glGetIntegerv(GL_MAX_DRAW_BUFFERS, &maxColorBuffers);
    GLenum* bufferMappings = new GLenum[maxColorBuffers];

Robert Menzel's avatar
Robert Menzel committed
129
    int attachments = std::min( maxColorBuffers, (int) mColorAttachments.size() );
130

Robert Menzel's avatar
Robert Menzel committed
131
    for (int i = 0; i < maxColorBuffers; ++i) {
Robert Menzel's avatar
Robert Menzel committed
132
133
        bufferMappings[i] = GL_NONE;
    }
Robert Menzel's avatar
Robert Menzel committed
134
    for (int i = 0; i < attachments; ++i) {
Janis Born's avatar
Janis Born committed
135
136
137
138
139
        if (bufferMappings[ mColorAttachments[i].location ] != GL_NONE) {
            Utils::warning() << "FBO: Attachment mapping collision: Location " << mColorAttachments[i].location;
            Utils::warning() << " maps to both attachments " << mColorAttachments[i].name;
            Utils::warning() << " and " << mColorAttachments[bufferMappings[mColorAttachments[i].location] - GL_COLOR_ATTACHMENT0].name << std::endl;
        }
Robert Menzel's avatar
Robert Menzel committed
140
141
        bufferMappings[ mColorAttachments[i].location ] = GL_COLOR_ATTACHMENT0 + i;
    }
142

Robert Menzel's avatar
Robert Menzel committed
143
    // debug:
Janis Born's avatar
Janis Born committed
144
145
    /*
    Utils::debug() << "remapAttachments: " << std::endl;
Robert Menzel's avatar
Robert Menzel committed
146
147
148
149
150
    for (unsigned int i = 0; i < maxColorBuffers; ++i) {
        if (bufferMappings[i] == GL_NONE) {
            Utils::debug() << "bufferMappings["<<i<<"] GL_NONE" << std::endl;
        } else {
            Utils::debug() << "bufferMappings["<<i<<"] "<< bufferMappings[i]-GL_COLOR_ATTACHMENT0 << std::endl;
151
152
        }
    }
Janis Born's avatar
Janis Born committed
153
    */
Robert Menzel's avatar
Robert Menzel committed
154
155
156
157
    // end debug

    bind(); // glDrawBuffers will get part of the FBO state!
    glDrawBuffers( maxColorBuffers, bufferMappings );
Janis Born's avatar
Janis Born committed
158
159

    delete[] bufferMappings;
160
161
}

Robert Menzel's avatar
Robert Menzel committed
162

163
164
165
166
167
168
169
170
void FrameBufferObject::setAttachmentLocations(ConstSharedLocationMappings _locationMappings)
{
    bool needsUpdate = false;

    for(AttachmentVec::size_type i = 0; i < mColorAttachments.size(); i++)
    {
        int_t location = _locationMappings->getLocation(mColorAttachments[i].name);

Robert Menzel's avatar
Robert Menzel committed
171
        if (location != -1) // is a mapping by that name specified?
172
        {
Robert Menzel's avatar
Robert Menzel committed
173
            mColorAttachments[i].location = location;
174
175
176
177
            needsUpdate = true;
        }
    }

Robert Menzel's avatar
Robert Menzel committed
178
    if(needsUpdate) remapAttachments();
179
180
181
}


Robert Menzel's avatar
Robert Menzel committed
182
183
184
SharedLocationMappings FrameBufferObject::getAttachmentLocations()
{
    SharedLocationMappings locationMap = SharedLocationMappings( new LocationMappings() );
185

Robert Menzel's avatar
Robert Menzel committed
186
    for (AttachmentVec::size_type i = 0; i < mColorAttachments.size(); i++)
187
    {
Robert Menzel's avatar
Robert Menzel committed
188
189
        locationMap->setLocation( mColorAttachments[i].name, mColorAttachments[i].location );
        //ACGL::Utils::debug() << "locationMap->setLocation( "<<mColorAttachments[i].name<<", "<<mColorAttachments[i].location<<" );"<<std::endl;
190
191
    }

Robert Menzel's avatar
Robert Menzel committed
192
    return locationMap;
193
}