]> git.tdb.fi Git - libs/gl.git/blob - source/framebuffer.cpp
Add Vector3 and Vector4 classes
[libs/gl.git] / source / framebuffer.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include "extension.h"
9 #include "ext_framebuffer_object.h"
10 #include "framebuffer.h"
11 #include "renderbuffer.h"
12 #include "texture2d.h"
13
14 namespace Msp {
15 namespace GL {
16
17 Framebuffer::Framebuffer()
18 {
19         static RequireExtension _ext("GL_EXT_framebuffer_object");
20
21         glGenFramebuffersEXT(1, &id);
22         bind();
23 }
24
25 Framebuffer::~Framebuffer()
26 {
27         glDeleteFramebuffersEXT(1, &id);
28 }
29
30 void Framebuffer::bind() const
31 {
32         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id);
33         cur_fbo=this;
34 }
35
36 void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf)
37 {
38         maybe_bind();
39         glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, attch, GL_RENDERBUFFER_EXT, rbuf.get_id());
40 }
41
42 void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, int level)
43 {
44         maybe_bind();
45         glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, attch, tex.get_target(), tex.get_id(), level);
46 }
47
48 FramebufferStatus Framebuffer::check_status() const
49 {
50         maybe_bind();
51         return static_cast<FramebufferStatus>(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
52 }
53
54 const Framebuffer *Framebuffer::current()
55 {
56         return cur_fbo;
57 }
58
59 void Framebuffer::unbind()
60 {
61         if(cur_fbo)
62         {
63                 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
64                 cur_fbo=0;
65         }
66 }
67
68 void Framebuffer::maybe_bind() const
69 {
70         if(cur_fbo!=this)
71                 bind();
72 }
73
74 const Framebuffer *Framebuffer::cur_fbo=0;
75
76 } // namespace GL
77 } // namespace Msp