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