]> git.tdb.fi Git - libs/gl.git/blob - source/texturing.cpp
Replace Texturing::Attachment with a simple Texture pointer
[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 void Texturing::bind_attachment(unsigned i) const
41 {
42         if(const Texture *tex = attachments[i])
43                 tex->bind_to(i);
44         else
45                 Texture::unbind_from(i);
46 }
47
48 void Texturing::unbind_attachment(unsigned i)
49 {
50         Texture::unbind_from(i);
51 }
52
53 void Texturing::bind() const
54 {
55         const Texturing *old = current();
56         if(set_current(this))
57         {
58                 for(unsigned i=0; i<attachments.size(); ++i)
59                         bind_attachment(i);
60                 if(old)
61                 {
62                         for(unsigned i=attachments.size(); i<old->attachments.size(); ++i)
63                                 unbind_attachment(i);
64                 }
65         }
66 }
67
68 void Texturing::unbind()
69 {
70         const Texturing *old = current();
71         if(set_current(0))
72         {
73                 for(unsigned i=old->attachments.size(); i--;)
74                         unbind_attachment(i);
75         }
76 }
77
78 } // namespace GL
79 } // namespace Msp;