]> git.tdb.fi Git - libs/gl.git/blob - source/texturing.cpp
Add missing includes
[libs/gl.git] / source / texturing.cpp
1 #include "texture.h"
2 #include "texturing.h"
3 #include "texunit.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9
10 Texturing::~Texturing()
11 {
12         if(current()==this)
13                 unbind();
14 }
15
16 void Texturing::attach(unsigned attch, const Texture &tex)
17 {
18         set_attachment(attch, &tex);
19 }
20
21 void Texturing::detach(unsigned attch)
22 {
23         set_attachment(attch, 0);
24 }
25
26 void Texturing::set_attachment(unsigned attch, const Texture *tex)
27 {
28         if(attch>=TexUnit::get_n_units())
29                 throw out_of_range("Texturing::set_attachment");
30
31         if(attachments.size()<=attch)
32                 attachments.resize(attch+1);
33
34         attachments[attch] = tex;
35
36         if(current()==this)
37                 bind_attachment(attch);
38 }
39
40 const Texture *Texturing::get_attached_texture(unsigned i) const
41 {
42         return i<attachments.size() ? attachments[i] : 0;
43 }
44
45 void Texturing::bind_attachment(unsigned i) const
46 {
47         if(const Texture *tex = attachments[i])
48                 tex->bind_to(i);
49         else
50                 Texture::unbind_from(i);
51 }
52
53 void Texturing::unbind_attachment(unsigned i)
54 {
55         Texture::unbind_from(i);
56 }
57
58 void Texturing::bind() const
59 {
60         const Texturing *old = current();
61         if(set_current(this))
62         {
63                 for(unsigned i=0; i<attachments.size(); ++i)
64                         bind_attachment(i);
65                 if(old)
66                 {
67                         for(unsigned i=attachments.size(); i<old->attachments.size(); ++i)
68                                 unbind_attachment(i);
69                 }
70         }
71 }
72
73 void Texturing::unbind()
74 {
75         const Texturing *old = current();
76         if(set_current(0))
77         {
78                 for(unsigned i=old->attachments.size(); i--;)
79                         unbind_attachment(i);
80         }
81 }
82
83 } // namespace GL
84 } // namespace Msp;