summaryrefslogtreecommitdiff
path: root/vcl/opengl/framebuffer.cxx
blob: 403c3792269570bd1cfa0244c216a2a270b56661 (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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <sal/log.hxx>

#include <opengl/framebuffer.hxx>

#include <vcl/opengl/OpenGLHelper.hxx>

OpenGLFramebuffer::OpenGLFramebuffer() :
    mnId( 0 ),
    mnWidth( 0 ),
    mnHeight( 0 ),
    mnAttachedTexture( 0 ),
    mpPrevFramebuffer( NULL ),
    mpNextFramebuffer( NULL )
{
    glGenFramebuffers( 1, &mnId );
    VCL_GL_INFO( "vcl.opengl", "Created framebuffer " << (int)mnId );
}

OpenGLFramebuffer::~OpenGLFramebuffer()
{
    glDeleteFramebuffers( 1, &mnId );
}

void OpenGLFramebuffer::Bind()
{
    VCL_GL_INFO( "vcl.opengl", "Binding framebuffer " << (int)mnId );
    glBindFramebuffer( GL_FRAMEBUFFER, mnId );
    CHECK_GL_ERROR();
}

void OpenGLFramebuffer::Unbind()
{
    glBindFramebuffer( GL_FRAMEBUFFER, 0 );
    VCL_GL_INFO( "vcl.opengl", "Binding default framebuffer" );
    CHECK_GL_ERROR();
}

bool OpenGLFramebuffer::IsFree() const
{
    return !mnAttachedTexture;
}

bool OpenGLFramebuffer::IsAttached( GLuint nTexture ) const
{
    return mnAttachedTexture == nTexture;
}

bool OpenGLFramebuffer::IsAttached( const OpenGLTexture& rTexture ) const
{
    return mnAttachedTexture == rTexture.Id();
}

void OpenGLFramebuffer::AttachTexture( const OpenGLTexture& rTexture )
{
    if( rTexture.Id() == mnAttachedTexture )
        return;

    VCL_GL_INFO( "vcl.opengl", "Attaching texture " << rTexture.Id() << " to framebuffer " << (int)mnId );
    mnAttachedTexture = rTexture.Id();
    mnWidth = rTexture.GetWidth();
    mnHeight = rTexture.GetHeight();
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mnAttachedTexture, 0);
    if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
    {
        SAL_WARN("vcl.opengl", "Framebuffer incomplete");
    }
    CHECK_GL_ERROR();
}

void OpenGLFramebuffer::DetachTexture()
{
    if( mnAttachedTexture != 0 )
    {
        CHECK_GL_ERROR();
        mnAttachedTexture = 0;
        glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0 );
        CHECK_GL_ERROR();
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */