]> git.tdb.fi Git - libs/gl.git/blob - source/texturing.cpp
Remove a number of rarely used legacy features
[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 = tex;
35
36         if(current()==this)
37                 bind_attachment(attch);
38 }
39
40 void Texturing::bind_attachment(unsigned i) const
41 {
42         const Attachment &attch = attachments[i];
43         if(attch.tex)
44                 attch.tex->bind_to(i);
45         else
46                 Texture::unbind_from(i);
47 }
48
49 void Texturing::unbind_attachment(unsigned i)
50 {
51         Texture::unbind_from(i);
52 }
53
54 void Texturing::bind() const
55 {
56         const Texturing *old = current();
57         if(set_current(this))
58         {
59                 for(unsigned i=0; i<attachments.size(); ++i)
60                         bind_attachment(i);
61                 if(old)
62                 {
63                         for(unsigned i=attachments.size(); i<old->attachments.size(); ++i)
64                                 unbind_attachment(i);
65                 }
66         }
67 }
68
69 void Texturing::unbind()
70 {
71         const Texturing *old = current();
72         if(set_current(0))
73         {
74                 for(unsigned i=old->attachments.size(); i--;)
75                         unbind_attachment(i);
76         }
77 }
78
79
80 Texturing::Attachment::Attachment():
81         tex(0)
82 { }
83
84 } // namespace GL
85 } // namespace Msp;