]> git.tdb.fi Git - libs/gl.git/blob - source/texturing.cpp
Change Texturing to store index-texture pairs
[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 unit, const Texture *tex)
27 {
28         if(unit>=TexUnit::get_n_units())
29                 throw out_of_range("Texturing::set_attachment");
30
31         if(tex)
32         {
33                 vector<Attachment>::iterator i;
34                 for(i=attachments.begin(); (i!=attachments.end() && i->unit<=unit); ++i)
35                         if(i->unit==unit)
36                         {
37                                 i->texture = tex;
38                                 if(current()==this)
39                                         tex->bind_to(unit);
40                                 return;
41                         }
42
43                 attachments.insert(i, Attachment(unit, tex));
44                 if(current()==this)
45                         tex->bind_to(unit);
46         }
47         else
48         {
49                 for(vector<Attachment>::iterator i=attachments.begin(); (i!=attachments.end() && i->unit<=unit); ++i)
50                         if(i->unit==unit)
51                         {
52                                 attachments.erase(i);
53                                 if(current()==this)
54                                         Texture::unbind_from(unit);
55                                 return;
56                         }
57         }
58 }
59
60 const Texture *Texturing::get_attached_texture(unsigned unit) const
61 {
62         for(vector<Attachment>::const_iterator i=attachments.begin(); (i!=attachments.end() && i->unit<=unit); ++i)
63                 if(i->unit==unit)
64                         return i->texture;
65         return 0;
66 }
67
68 void Texturing::bind() const
69 {
70         const Texturing *old = current();
71         if(set_current(this))
72         {
73                 if(old)
74                 {
75                         vector<Attachment>::const_iterator i = attachments.begin();
76                         vector<Attachment>::const_iterator j = old->attachments.begin();
77                         while(i!=attachments.end() || j!=old->attachments.end())
78                         {
79                                 if(i!=attachments.end() && (j==old->attachments.end() || i->unit<=j->unit))
80                                 {
81                                         i->texture->bind_to(i->unit);
82                                         if(j!=old->attachments.end() && j->unit==i->unit)
83                                                 ++j;
84                                         ++i;
85                                 }
86                                 else
87                                 {
88                                         Texture::unbind_from(j->unit);
89                                         ++j;
90                                 }
91                         }
92                 }
93                 else
94                 {
95                         for(vector<Attachment>::const_iterator i=attachments.begin(); i!=attachments.end(); ++i)
96                                 i->texture->bind_to(i->unit);
97                 }
98         }
99 }
100
101 void Texturing::unbind()
102 {
103         const Texturing *old = current();
104         if(set_current(0))
105         {
106                 for(vector<Attachment>::const_iterator i=old->attachments.begin(); i!=old->attachments.end(); ++i)
107                         Texture::unbind_from(i->unit);
108         }
109 }
110
111
112 Texturing::Attachment::Attachment(unsigned u, const Texture *t):
113         unit(u),
114         texture(t)
115 { }
116
117 } // namespace GL
118 } // namespace Msp;