1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <sal/log.hxx>
12 #include <opengl/framebuffer.hxx>
14 #include <vcl/opengl/OpenGLHelper.hxx>
16 OpenGLFramebuffer::OpenGLFramebuffer() :
20 mnAttachedTexture( 0 ),
21 mpPrevFramebuffer( nullptr )
23 glGenFramebuffers( 1, &mnId
);
25 VCL_GL_INFO( "Created framebuffer " << static_cast<int>(mnId
) );
28 OpenGLFramebuffer::~OpenGLFramebuffer()
30 glDeleteFramebuffers( 1, &mnId
);
31 VCL_GL_INFO( "Deleted framebuffer " << static_cast<int>(mnId
) );
35 void OpenGLFramebuffer::Bind(GLenum eTarget
)
37 VCL_GL_INFO( "Binding framebuffer " << static_cast<int>(mnId
) );
38 glBindFramebuffer(eTarget
, mnId
);
42 void OpenGLFramebuffer::Unbind(GLenum eTarget
)
44 glBindFramebuffer(eTarget
, 0);
46 VCL_GL_INFO( "Binding default framebuffer" );
49 bool OpenGLFramebuffer::IsFree() const
51 return !mnAttachedTexture
;
54 bool OpenGLFramebuffer::IsAttached( GLuint nTexture
) const
56 return mnAttachedTexture
== nTexture
;
59 bool OpenGLFramebuffer::IsAttached( const OpenGLTexture
& rTexture
) const
61 return mnAttachedTexture
== rTexture
.Id();
64 void OpenGLFramebuffer::AttachTexture( const OpenGLTexture
& rTexture
)
66 if( rTexture
.Id() == mnAttachedTexture
)
69 VCL_GL_INFO( "Attaching texture " << rTexture
.Id() << " to framebuffer " << static_cast<int>(mnId
) );
70 mnAttachedTexture
= rTexture
.Id();
71 mnWidth
= rTexture
.GetWidth();
72 mnHeight
= rTexture
.GetHeight();
73 glFramebufferTexture2D(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
, GL_TEXTURE_2D
, mnAttachedTexture
, 0);
76 GLuint nStencil
= rTexture
.StencilId();
79 VCL_GL_INFO( "Attaching stencil " << nStencil
<< " to framebuffer " << static_cast<int>(mnId
) );
80 glFramebufferRenderbuffer( GL_FRAMEBUFFER
, GL_STENCIL_ATTACHMENT
,
81 GL_RENDERBUFFER
, nStencil
);
85 GLenum status
= glCheckFramebufferStatus(GL_FRAMEBUFFER
);
87 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
89 SAL_WARN("vcl.opengl", "Framebuffer incomplete");
93 void OpenGLFramebuffer::DetachTexture()
95 if( mnAttachedTexture
!= 0 )
97 mnAttachedTexture
= 0;
98 glFramebufferTexture2D( GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
, GL_TEXTURE_2D
, 0, 0 );
101 // FIXME: we could make this conditional on having a stencil ?
102 glFramebufferRenderbuffer( GL_FRAMEBUFFER
, GL_STENCIL_ATTACHMENT
,
103 GL_RENDERBUFFER
, 0 );
108 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */